tools/power/cpupower/utils/helpers/sysfs.c
Source file repositories/reference/linux-study-clean/tools/power/cpupower/utils/helpers/sysfs.c
File Facts
- System
- Linux kernel
- Corpus path
tools/power/cpupower/utils/helpers/sysfs.c- Extension
.c- Size
- 10591 bytes
- Lines
- 472
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdio.herrno.hstdlib.hstring.hsys/types.hsys/stat.hfcntl.hunistd.hhelpers/sysfs.h
Detected Declarations
enum idlestate_valueenum idlestate_stringenum cpuidle_stringfunction sysfs_read_filefunction sysfs_is_cpu_onlinefunction sysfs_idlestate_file_existsfunction sysfs_idlestate_read_filefunction sysfs_idlestate_write_filefunction sysfs_idlestate_get_one_valuefunction sysfs_is_idlestate_disabledfunction sysfs_idlestate_disablefunction sysfs_get_idlestate_latencyfunction sysfs_get_idlestate_usagefunction sysfs_get_idlestate_timefunction sysfs_get_idlestate_countfunction sysfs_cpuidle_read_filefunction sysfs_get_schedfunction sysfs_set_sched
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0-only
/*
* (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
* (C) 2011 Thomas Renninger <trenn@novell.com> Novell Inc.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "helpers/sysfs.h"
unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen)
{
int fd;
ssize_t numread;
fd = open(path, O_RDONLY);
if (fd == -1)
return 0;
numread = read(fd, buf, buflen - 1);
if (numread < 1) {
close(fd);
return 0;
}
buf[numread] = '\0';
close(fd);
return (unsigned int) numread;
}
/*
* Detect whether a CPU is online
*
* Returns:
* 1 -> if CPU is online
* 0 -> if CPU is offline
* negative errno values in error case
*/
int sysfs_is_cpu_online(unsigned int cpu)
{
char path[SYSFS_PATH_MAX];
int fd;
ssize_t numread;
unsigned long long value;
char linebuf[MAX_LINE_LEN];
char *endp;
struct stat statbuf;
snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u", cpu);
if (stat(path, &statbuf) != 0)
return 0;
/*
* kernel without CONFIG_HOTPLUG_CPU
* -> cpuX directory exists, but not cpuX/online file
*/
snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/online", cpu);
if (stat(path, &statbuf) != 0)
return 1;
fd = open(path, O_RDONLY);
if (fd == -1)
return -errno;
numread = read(fd, linebuf, MAX_LINE_LEN - 1);
if (numread < 1) {
close(fd);
return -EIO;
}
linebuf[numread] = '\0';
close(fd);
value = strtoull(linebuf, &endp, 0);
if (value > 1)
return -EINVAL;
return value;
}
/* CPUidle idlestate specific /sys/devices/system/cpu/cpuX/cpuidle/ access */
Annotation
- Immediate include surface: `stdio.h`, `errno.h`, `stdlib.h`, `string.h`, `sys/types.h`, `sys/stat.h`, `fcntl.h`, `unistd.h`.
- Detected declarations: `enum idlestate_value`, `enum idlestate_string`, `enum cpuidle_string`, `function sysfs_read_file`, `function sysfs_is_cpu_online`, `function sysfs_idlestate_file_exists`, `function sysfs_idlestate_read_file`, `function sysfs_idlestate_write_file`, `function sysfs_idlestate_get_one_value`, `function sysfs_is_idlestate_disabled`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.