• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <string.h>
3 
4 #include "common.h"
5 #include "bitmask.h"
6 #include "cpuset.h"
7 #include "meminfo.h"
8 
9 #define LIST_PRESENT_MEM_FILE	"/sys/devices/system/node/possible"
10 #define LIST_ONLINE_MEM_FILE	"/sys/devices/system/node/online"
11 
12 int nmems;
13 int mems_nbits;
14 
15 /*
16  * get the bitmask of the online nodes
17  *
18  * return value: 0  - success
19  * 		 -1 - failed
20  */
online_memmask(struct bitmask * memmask)21 int online_memmask(struct bitmask *memmask)
22 {
23 	FILE *fp = NULL;
24 	char buf[BUFFSIZE];
25 
26 	if (memmask == NULL)
27 		return -1;
28 
29 	/*
30 	 * open file /sys/devices/system/node/online and get online
31 	 * nodelist
32 	 */
33 	if ((fp = fopen(LIST_ONLINE_MEM_FILE, "r")) == NULL)
34 		return -1;
35 
36 	if (fgets(buf, sizeof(buf), fp) == NULL) {
37 		fclose(fp);
38 		return -1;
39 	}
40 
41 	fclose(fp);
42 
43 	/* parse present nodelist to bitmap */
44 	buf[strlen(buf) - 1] = '\0';
45 	if (bitmask_parselist(buf, memmask) != 0)
46 		return -1;
47 
48 	return 0;
49 }
50 
51 /*
52  * get the bitmask of the present nodes including offline nodes
53  *
54  * return value: 0  - success
55  * 		 -1 - failed
56  */
present_memmask(struct bitmask * memmask)57 int present_memmask(struct bitmask *memmask)
58 {
59 	FILE *fp = NULL;
60 	char buf[BUFFSIZE];
61 
62 	if (memmask == NULL)
63 		return -1;
64 
65 	/*
66 	 * open file /sys/devices/system/node/possible and get present
67 	 * nodelist
68 	 */
69 	if ((fp = fopen(LIST_PRESENT_MEM_FILE, "r")) == NULL)
70 		return -1;
71 
72 	if (fgets(buf, sizeof(buf), fp) == NULL) {
73 		fclose(fp);
74 		return -1;
75 	}
76 
77 	fclose(fp);
78 
79 	/* parse present nodelist to bitmap */
80 	buf[strlen(buf) - 1] = '\0';
81 	if (bitmask_parselist(buf, memmask) != 0)
82 		return -1;
83 
84 	return 0;
85 }
86 
87 /*
88  * get the number of the nodes including offline nodes
89  */
get_nmems(void)90 int get_nmems(void)
91 {
92 	struct bitmask *bmp = NULL;
93 	int n = 0;
94 
95 	/* get the bitmask's len */
96 	if (mems_nbits <= 0) {
97 		mems_nbits = cpuset_mems_nbits();
98 		if (mems_nbits <= 0)
99 			return -1;
100 	}
101 
102 	/* allocate the space for bitmask */
103 	bmp = bitmask_alloc(mems_nbits);
104 	if (bmp == NULL)
105 		return -1;
106 
107 	if (present_memmask(bmp)) {
108 		bitmask_free(bmp);
109 		return -1;
110 	}
111 
112 	/* Numbwe of highest set bit +1 is the number of the nodes */
113 	if (bitmask_weight(bmp) <= 0) {
114 		bitmask_free(bmp);
115 		return -1;
116 	}
117 
118 	n = bitmask_last(bmp) + 1;
119 	bitmask_free(bmp);
120 
121 	return n;
122 }
123