• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /* SPDX-License-Identifier: GPL-2.0-or-later */
2  /*
3   * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
4   */
5  
6  #ifndef PROCESS_MADVISE_H__
7  #define PROCESS_MADVISE_H__
8  
9  #include <stdio.h>
10  #include <stdlib.h>
11  #include <string.h>
12  #include <unistd.h>
13  #include "tst_safe_stdio.h"
14  
15  struct addr_mapping {
16  	int size;
17  	int rss;
18  	int pss;
19  	int shared_clean;
20  	int shared_dirty;
21  	int private_clean;
22  	int private_dirty;
23  	int referenced;
24  	int anonymous;
25  	int anon_huge_pages;
26  	int shmem_huge_pages;
27  	int shmem_pmd_mapped;
28  	int swap;
29  	int kernel_page_size;
30  	int mmu_page_size;
31  	int locked;
32  	int protection_key;
33  };
34  
read_address_mapping(unsigned long address,struct addr_mapping * mapping)35  static inline void read_address_mapping(unsigned long address, struct addr_mapping *mapping)
36  {
37  	FILE *f;
38  	int found = 0;
39  	char label[BUFSIZ];
40  	char line[BUFSIZ];
41  	char smaps[BUFSIZ];
42  	char ptr_str[BUFSIZ];
43  	int value;
44  
45  	snprintf(smaps, BUFSIZ, "/proc/%i/smaps", getpid());
46  	snprintf(ptr_str, BUFSIZ, "%lx", address);
47  
48  	f = SAFE_FOPEN(smaps, "r");
49  
50  	while (fgets(line, BUFSIZ, f) != NULL) {
51  		if (strncmp(ptr_str, line, strlen(ptr_str)) == 0)
52  			found = 1;
53  
54  		if (!found)
55  			continue;
56  
57  		if (found && strcmp(line, "VmFlags") >= 0)
58  			break;
59  
60  		if (sscanf(line, "%31[^:]: %d", label, &value) > 0) {
61  			if (strcmp(label, "Size") == 0)
62  				mapping->size = value;
63  			else if (strcmp(label, "Rss") == 0)
64  				mapping->rss = value;
65  			else if (strcmp(label, "Pss") == 0)
66  				mapping->pss = value;
67  			else if (strcmp(label, "Shared_Clean") == 0)
68  				mapping->shared_clean = value;
69  			else if (strcmp(label, "Shared_Dirty") == 0)
70  				mapping->shared_dirty = value;
71  			else if (strcmp(label, "Private_Clean") == 0)
72  				mapping->private_clean = value;
73  			else if (strcmp(label, "Private_Dirty") == 0)
74  				mapping->private_dirty = value;
75  			else if (strcmp(label, "Referenced") == 0)
76  				mapping->referenced = value;
77  			else if (strcmp(label, "Anonymous") == 0)
78  				mapping->anonymous = value;
79  			else if (strcmp(label, "AnonHugePages") == 0)
80  				mapping->anon_huge_pages = value;
81  			else if (strcmp(label, "ShmemHugePages") == 0)
82  				mapping->shmem_huge_pages = value;
83  			else if (strcmp(label, "ShmemPmdMapped") == 0)
84  				mapping->shmem_pmd_mapped = value;
85  			else if (strcmp(label, "Swap") == 0)
86  				mapping->swap = value;
87  			else if (strcmp(label, "KernelPageSize") == 0)
88  				mapping->kernel_page_size = value;
89  			else if (strcmp(label, "MMUPageSize") == 0)
90  				mapping->mmu_page_size = value;
91  			else if (strcmp(label, "Locked") == 0)
92  				mapping->locked = value;
93  			else if (strcmp(label, "ProtectionKey") == 0)
94  				mapping->protection_key = value;
95  		}
96  	}
97  
98  	SAFE_FCLOSE(f);
99  }
100  
101  #endif
102