1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <dirent.h> #include <fcntl.h>
/** * 读取sysfs文件内容 */ int read_sysfs_file(const char *path, char *buffer, size_t buffer_size) { int fd; ssize_t bytes_read; fd = open(path, O_RDONLY); if (fd == -1) { return -1; } bytes_read = read(fd, buffer, buffer_size - 1); if (bytes_read == -1) { close(fd); return -1; } buffer[bytes_read] = '\0'; // 移除末尾的换行符 if (bytes_read > 0 && buffer[bytes_read - 1] == '\n') { buffer[bytes_read - 1] = '\0'; } close(fd); return 0; }
/** * 显示CPU信息 */ void show_cpu_info() { DIR *dir; struct dirent *entry; char path[256]; char buffer[256]; printf("=== CPU信息 ===\n"); // 读取CPU基本信息 if (read_sysfs_file("/sys/devices/system/cpu/online", buffer, sizeof(buffer)) == 0) { printf("在线CPU: %s\n", buffer); } if (read_sysfs_file("/sys/devices/system/cpu/offline", buffer, sizeof(buffer)) == 0) { if (strlen(buffer) > 0) { printf("离线CPU: %s\n", buffer); } } // 遍历CPU目录 dir = opendir("/sys/devices/system/cpu"); if (dir) { int cpu_count = 0; while ((entry = readdir(dir)) != NULL) { if (strncmp(entry->d_name, "cpu", 3) == 0 && entry->d_name[3] >= '0' && entry->d_name[3] <= '9') { cpu_count++; // 读取CPU频率信息 snprintf(path, sizeof(path), "/sys/devices/system/cpu/%s/cpufreq/scaling_cur_freq", entry->d_name); if (read_sysfs_file(path, buffer, sizeof(buffer)) == 0) { long freq_khz = atol(buffer); printf("CPU %s 当前频率: %.2f MHz\n", entry->d_name, freq_khz / 1000.0); } // 读取CPU在线状态 snprintf(path, sizeof(path), "/sys/devices/system/cpu/%s/online", entry->d_name); if (read_sysfs_file(path, buffer, sizeof(buffer)) == 0) { printf("CPU %s 在线状态: %s\n", entry->d_name, atoi(buffer) ? "在线" : "离线"); } } } printf("CPU总数: %d\n", cpu_count); closedir(dir); } printf("\n"); }
/** * 显示内存信息 */ void show_memory_info() { char buffer[256]; printf("=== 内存信息 ===\n"); // 读取内存块信息 if (read_sysfs_file("/sys/devices/system/memory/block_size_bytes", buffer, sizeof(buffer)) == 0) { unsigned long block_size = strtoul(buffer, NULL, 16); printf("内存块大小: %lu 字节 (%.2f MB)\n", block_size, block_size / (1024.0 * 1024.0)); } // 遍历内存块 DIR *dir = opendir("/sys/devices/system/memory"); if (dir) { struct dirent *entry; int online_count = 0, total_count = 0; while ((entry = readdir(dir)) != NULL) { if (strncmp(entry->d_name, "memory", 6) == 0) { total_count++; char path[256]; snprintf(path, sizeof(path), "/sys/devices/system/memory/%s/state", entry->d_name); if (read_sysfs_file(path, buffer, sizeof(buffer)) == 0) { if (strcmp(buffer, "online") == 0) { online_count++; } } } } printf("内存块总数: %d\n", total_count); printf("在线内存块: %d\n", online_count); printf("离线内存块: %d\n", total_count - online_count); closedir(dir); } printf("\n"); }
/** * 显示块设备信息 */ void show_block_devices() { printf("=== 块设备信息 ===\n"); DIR *dir = opendir("/sys/block"); if (dir) { struct dirent *entry; printf("块设备列表:\n"); while ((entry = readdir(dir)) != NULL) { if (entry->d_name[0] != '.') { char path[256]; char buffer[256]; // 读取设备大小 snprintf(path, sizeof(path), "/sys/block/%s/size", entry->d_name); if (read_sysfs_file(path, buffer, sizeof(buffer)) == 0) { unsigned long sectors = atol(buffer); double size_gb = sectors * 512.0 / (1024.0 * 1024.0 * 1024.0); printf(" %s: %.2f GB (%lu 扇区)\n", entry->d_name, size_gb, sectors); } else { printf(" %s: (无法获取大小)\n", entry->d_name); } // 读取设备类型 snprintf(path, sizeof(path), "/sys/block/%s/queue/rotational", entry->d_name); if (read_sysfs_file(path, buffer, sizeof(buffer)) == 0) { if (atoi(buffer) == 1) { printf(" 类型: 机械硬盘\n"); } else { printf(" 类型: 固态硬盘\n"); } } } } closedir(dir); } printf("\n"); }
/** * 演示基础sysfs使用方法 */ int demo_sysfs_basic() { printf("=== 基础sysfs使用示例 ===\n"); // 检查sysfs是否挂载 if (access("/sys", F_OK) == -1) { printf("sysfs未挂载或不可访问\n"); return -1; } printf("sysfs挂载点: /sys\n"); // 显示CPU信息 show_cpu_info(); // 显示内存信息 show_memory_info(); // 显示块设备信息 show_block_devices(); // 演示读取特定属性 printf("=== 特定属性读取演示 ===\n"); struct { const char *path; const char *description; } attributes[] = { {"/sys/class/dmi/id/product_name", "产品名称"}, {"/sys/class/dmi/id/product_version", "产品版本"}, {"/sys/class/dmi/id/bios_vendor", "BIOS厂商"}, {"/sys/class/dmi/id/bios_version", "BIOS版本"}, {"/sys/kernel/mm/transparent_hugepage/enabled", "透明大页状态"}, {NULL, NULL} }; for (int i = 0; attributes[i].path; i++) { char buffer[256]; if (read_sysfs_file(attributes[i].path, buffer, sizeof(buffer)) == 0) { printf("%s: %s\n", attributes[i].description, buffer); } else { printf("%s: 无法读取 (%s)\n", attributes[i].description, strerror(errno)); } } return 0; }
int main() { return demo_sysfs_basic(); }
|