tools/power/cpupower/lib/powercap.c
Source file repositories/reference/linux-study-clean/tools/power/cpupower/lib/powercap.c
File Facts
- System
- Linux kernel
- Corpus path
tools/power/cpupower/lib/powercap.c- Extension
.c- Size
- 6462 bytes
- Lines
- 308
- 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
sys/types.hsys/stat.hunistd.hstdlib.hstring.hfcntl.hstdio.hdirent.hpowercap.h
Detected Declarations
enum powercap_get64function sysfs_read_filefunction sysfs_get_enabledfunction powercap_get_enabledfunction powercap_set_enabledfunction powercap_get_driverfunction sysfs_powercap_get64_valfunction powercap_get_max_energy_range_ujfunction powercap_get_energy_ujfunction powercap_get_max_power_range_uwfunction powercap_get_power_uwfunction powercap_zone_get_enabledfunction powercap_zone_set_enabledfunction powercap_read_zonefunction powercap_walk_zones
Annotated Snippet
if (zone->children[i] == NULL) {
zone->children[i] = child_zone;
break;
}
if (i == POWERCAP_MAX_CHILD_ZONES - 1) {
free(child_zone);
fprintf(stderr, "Reached POWERCAP_MAX_CHILD_ZONES %d\n",
POWERCAP_MAX_CHILD_ZONES);
return -1;
}
}
strcpy(child_zone->sys_name, zone->sys_name);
strcat(child_zone->sys_name, "/");
strcat(child_zone->sys_name, dent->d_name);
child_zone->parent = zone;
if (zone->tree_depth >= POWERCAP_MAX_TREE_DEPTH) {
fprintf(stderr, "Maximum zone hierarchy depth[%d] reached\n",
POWERCAP_MAX_TREE_DEPTH);
ret = -1;
break;
}
powercap_read_zone(child_zone);
}
closedir(zone_dir);
return ret;
}
struct powercap_zone *powercap_init_zones(void)
{
int enabled;
struct powercap_zone *root_zone;
int ret;
char file[SYSFS_PATH_MAX] = PATH_TO_RAPL "/enabled";
ret = sysfs_get_enabled(file, &enabled);
if (ret)
return NULL;
if (!enabled)
return NULL;
root_zone = calloc(1, sizeof(struct powercap_zone));
if (!root_zone)
return NULL;
strcpy(root_zone->sys_name, "intel-rapl/intel-rapl:0");
powercap_read_zone(root_zone);
return root_zone;
}
/* Call function *f on the passed zone and all its children */
int powercap_walk_zones(struct powercap_zone *zone,
int (*f)(struct powercap_zone *zone))
{
int i, ret;
if (!zone)
return -1;
ret = f(zone);
if (ret)
return ret;
for (i = 0; i < POWERCAP_MAX_CHILD_ZONES; i++) {
if (zone->children[i] != NULL)
powercap_walk_zones(zone->children[i], f);
}
return 0;
}
Annotation
- Immediate include surface: `sys/types.h`, `sys/stat.h`, `unistd.h`, `stdlib.h`, `string.h`, `fcntl.h`, `stdio.h`, `dirent.h`.
- Detected declarations: `enum powercap_get64`, `function sysfs_read_file`, `function sysfs_get_enabled`, `function powercap_get_enabled`, `function powercap_set_enabled`, `function powercap_get_driver`, `function sysfs_powercap_get64_val`, `function powercap_get_max_energy_range_uj`, `function powercap_get_energy_uj`, `function powercap_get_max_power_range_uw`.
- 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.