tools/testing/selftests/cgroup/test_hugetlb_memcg.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/cgroup/test_hugetlb_memcg.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/cgroup/test_hugetlb_memcg.c- Extension
.c- Size
- 5806 bytes
- Lines
- 243
- 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.
Dependency Surface
linux/limits.hsys/mman.hstdio.hstdlib.hstring.hfcntl.hkselftest.hcgroup_util.h
Detected Declarations
function get_hugepage_sizefunction set_filefunction set_nr_hugepagesfunction check_firstfunction write_datafunction hugetlb_test_programfunction test_hugetlb_memcgfunction main
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <linux/limits.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "kselftest.h"
#include "cgroup_util.h"
#define ADDR ((void *)(0x0UL))
#define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB)
/* mapping 8 MBs == 4 hugepages */
#define LENGTH (8UL*1024*1024)
#define PROTECTION (PROT_READ | PROT_WRITE)
/* borrowed from mm/hmm-tests.c */
static long get_hugepage_size(void)
{
int fd;
char buf[2048];
int len;
char *p, *q, *path = "/proc/meminfo", *tag = "Hugepagesize:";
long val;
fd = open(path, O_RDONLY);
if (fd < 0) {
/* Error opening the file */
return -1;
}
len = read(fd, buf, sizeof(buf));
close(fd);
if (len < 0) {
/* Error in reading the file */
return -1;
}
if (len == sizeof(buf)) {
/* Error file is too large */
return -1;
}
buf[len] = '\0';
/* Search for a tag if provided */
if (tag) {
p = strstr(buf, tag);
if (!p)
return -1; /* looks like the line we want isn't there */
p += strlen(tag);
} else
p = buf;
val = strtol(p, &q, 0);
if (*q != ' ') {
/* Error parsing the file */
return -1;
}
return val;
}
static int set_file(const char *path, long value)
{
FILE *file;
int ret;
file = fopen(path, "w");
if (!file)
return -1;
ret = fprintf(file, "%ld\n", value);
fclose(file);
return ret;
}
static int set_nr_hugepages(long value)
{
return set_file("/proc/sys/vm/nr_hugepages", value);
}
static unsigned int check_first(char *addr)
{
return *(unsigned int *)addr;
}
static void write_data(char *addr)
{
unsigned long i;
Annotation
- Immediate include surface: `linux/limits.h`, `sys/mman.h`, `stdio.h`, `stdlib.h`, `string.h`, `fcntl.h`, `kselftest.h`, `cgroup_util.h`.
- Detected declarations: `function get_hugepage_size`, `function set_file`, `function set_nr_hugepages`, `function check_first`, `function write_data`, `function hugetlb_test_program`, `function test_hugetlb_memcg`, `function main`.
- 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.