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