| 44 | */ |
| 45 | |
| 46 | int get_cpu_items(struct cpu_item** p_item) |
| 47 | { |
| 48 | char cpu_path[128]; |
| 49 | char file_path[128]; |
| 50 | struct cpu_item* cpu_item = NULL; |
| 51 | struct stat stat_buf; |
| 52 | int i = 0; |
| 53 | |
| 54 | while(1) |
| 55 | { |
| 56 | FILE* fp; |
| 57 | int ret; |
| 58 | |
| 59 | sprintf(cpu_path, "/sys/devices/system/cpu/cpu%d/cpufreq", i); |
| 60 | |
| 61 | if(stat(cpu_path, &stat_buf) < 0) |
| 62 | break; |
| 63 | |
| 64 | cpu_item = ( struct cpu_item* )realloc(cpu_item, sizeof(struct cpu_item) * (i + 1)); |
| 65 | |
| 66 | cpu_item[i].cpu_id = i; |
| 67 | |
| 68 | ret = snprintf(file_path, 128, "%s/cpuinfo_max_freq", cpu_path); |
| 69 | |
| 70 | if(ret >= 128) |
| 71 | file_path[127] = 0x0; |
| 72 | |
| 73 | fp = fopen(file_path, "rb"); |
| 74 | |
| 75 | if(fp == NULL) |
| 76 | break; |
| 77 | |
| 78 | if(fscanf(fp, "%d", &cpu_item[i].max_freq) < 0) |
| 79 | { |
| 80 | fclose(fp); |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | fclose(fp); |
| 85 | |
| 86 | ret = snprintf(file_path, 128, "%s/related_cpus", cpu_path); |
| 87 | |
| 88 | if(ret >= 128) |
| 89 | file_path[127] = 0x0; |
| 90 | |
| 91 | fp = fopen(file_path, "rb"); |
| 92 | |
| 93 | if(fp == NULL) |
| 94 | break; |
| 95 | |
| 96 | if(fscanf(fp, "%d ", &cpu_item[i].cluster_leader) < 0) |
| 97 | { |
| 98 | fclose(fp); |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | fclose(fp); |
| 103 | |