tools/testing/selftests/acct/taskstats_fill_stats_tgid.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/acct/taskstats_fill_stats_tgid.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/acct/taskstats_fill_stats_tgid.c- Extension
.c- Size
- 9521 bytes
- Lines
- 376
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
errno.hlinux/genetlink.hlinux/netlink.hlinux/taskstats.hpthread.hstdbool.hstdint.hstdio.hstdlib.hstring.hsys/socket.hsys/types.htime.hunistd.hkselftest.h
Detected Declarations
struct worker_ctxfunction taskstats_nla_okfunction timespec_diff_nsfunction burn_cpu_for_nsfunction netlink_openfunction send_requestfunction get_family_idfunction get_taskstatsfunction cpu_totalfunction print_statsfunction main
Annotated Snippet
struct worker_ctx {
pthread_mutex_t lock;
pthread_cond_t cond;
bool ready;
bool release;
};
static unsigned long busy_sink;
static void *taskstats_nla_data(const struct nlattr *na)
{
return (void *)((char *)na + NLA_HDRLEN);
}
static bool taskstats_nla_ok(const struct nlattr *na, int remaining)
{
return remaining >= (int)sizeof(*na) &&
na->nla_len >= sizeof(*na) &&
na->nla_len <= remaining;
}
static struct nlattr *taskstats_nla_next(const struct nlattr *na, int *remaining)
{
int aligned_len = NLA_ALIGN(na->nla_len);
*remaining -= aligned_len;
return (struct nlattr *)((char *)na + aligned_len);
}
static uint64_t timespec_diff_ns(const struct timespec *start,
const struct timespec *end)
{
return (uint64_t)(end->tv_sec - start->tv_sec) * 1000000000ULL +
(uint64_t)(end->tv_nsec - start->tv_nsec);
}
static void burn_cpu_for_ns(uint64_t runtime_ns)
{
struct timespec start, now;
unsigned long acc = 0;
if (clock_gettime(CLOCK_MONOTONIC, &start)) {
perror("clock_gettime");
exit(EXIT_FAILURE);
}
do {
for (int i = 0; i < 100000; i++)
acc += i;
if (clock_gettime(CLOCK_MONOTONIC, &now)) {
perror("clock_gettime");
exit(EXIT_FAILURE);
}
} while (timespec_diff_ns(&start, &now) < runtime_ns);
busy_sink = acc;
}
static int netlink_open(void)
{
struct sockaddr_nl addr = {
.nl_family = AF_NETLINK,
.nl_pid = getpid(),
};
int fd;
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
if (fd < 0)
return -errno;
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
int err = -errno;
close(fd);
return err;
}
return fd;
}
static int send_request(int fd, void *buf, size_t len)
{
struct sockaddr_nl addr = {
.nl_family = AF_NETLINK,
};
if (sendto(fd, buf, len, 0, (struct sockaddr *)&addr, sizeof(addr)) < 0)
return -errno;
return 0;
Annotation
- Immediate include surface: `errno.h`, `linux/genetlink.h`, `linux/netlink.h`, `linux/taskstats.h`, `pthread.h`, `stdbool.h`, `stdint.h`, `stdio.h`.
- Detected declarations: `struct worker_ctx`, `function taskstats_nla_ok`, `function timespec_diff_ns`, `function burn_cpu_for_ns`, `function netlink_open`, `function send_request`, `function get_family_id`, `function get_taskstats`, `function cpu_total`, `function print_stats`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.