tools/power/cpupower/lib/cpuidle.c
Source file repositories/reference/linux-study-clean/tools/power/cpupower/lib/cpuidle.c
File Facts
- System
- Linux kernel
- Corpus path
tools/power/cpupower/lib/cpuidle.c- Extension
.c- Size
- 8966 bytes
- Lines
- 388
- 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.hcpuidle.hcpupower_intern.h
Detected Declarations
enum idlestate_valueenum idlestate_stringenum cpuidle_stringfunction cpuidle_state_file_existsfunction cpuidle_state_read_filefunction cpuidle_state_write_filefunction cpuidle_state_get_one_valuefunction cpuidle_is_state_disabledfunction cpuidle_state_disablefunction cpuidle_state_latencyfunction cpuidle_state_residencyfunction cpuidle_state_usagefunction cpuidle_state_timefunction cpuidle_state_countfunction sysfs_cpuidle_read_file
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 "cpuidle.h"
#include "cpupower_intern.h"
/*
* helper function to check whether a file under "../cpuX/cpuidle/stateX/" dir
* exists.
* For example the functionality to disable c-states was introduced in later
* kernel versions, this function can be used to explicitly check for this
* feature.
*
* returns 1 if the file exists, 0 otherwise.
*/
static
unsigned int cpuidle_state_file_exists(unsigned int cpu,
unsigned int idlestate,
const char *fname)
{
char path[SYSFS_PATH_MAX];
struct stat statbuf;
snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpuidle/state%u/%s",
cpu, idlestate, fname);
if (stat(path, &statbuf) != 0)
return 0;
return 1;
}
/*
* helper function to read file from /sys into given buffer
* fname is a relative path under "cpuX/cpuidle/stateX/" dir
* cstates starting with 0, C0 is not counted as cstate.
* This means if you want C1 info, pass 0 as idlestate param
*/
static
unsigned int cpuidle_state_read_file(unsigned int cpu,
unsigned int idlestate,
const char *fname, char *buf,
size_t buflen)
{
char path[SYSFS_PATH_MAX];
int fd;
ssize_t numread;
snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpuidle/state%u/%s",
cpu, idlestate, fname);
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;
}
/*
* helper function to write a new value to a /sys file
* fname is a relative path under "../cpuX/cpuidle/cstateY/" dir
*
* Returns the number of bytes written or 0 on error
*/
static
unsigned int cpuidle_state_write_file(unsigned int cpu,
unsigned int idlestate,
const char *fname,
const char *value, size_t len)
{
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 cpuidle_state_file_exists`, `function cpuidle_state_read_file`, `function cpuidle_state_write_file`, `function cpuidle_state_get_one_value`, `function cpuidle_is_state_disabled`, `function cpuidle_state_disable`, `function cpuidle_state_latency`.
- 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.