1 /************************************************************************ 2 Copyright (c) 2015, The Linux Foundation. All rights reserved. 3 4 Redistribution and use in source and binary forms, with or without 5 modification, are permitted provided that the following conditions are 6 met: 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above 10 copyright notice, this list of conditions and the following 11 disclaimer in the documentation and/or other materials provided 12 with the distribution. 13 * Neither the name of The Linux Foundation nor the names of its 14 contributors may be used to endorse or promote products derived 15 from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 ************************************************************************/ 29 30 /** 31 * @file datatop_cpu_stats_poll.c 32 * @brief Calls dtop_value_only_init for necessary cpu datapoints. 33 * 34 * File contains methods for determing number of cpu's online and calling 35 * correct initialization function to gather scaling_cur_freq data point 36 * for each cpu along with each cpu's online status. 37 */ 38 39 #include <unistd.h> 40 #include <stdio.h> 41 #include <dirent.h> 42 #include <string.h> 43 #include <stdlib.h> 44 #include <errno.h> 45 #include <sys/stat.h> 46 #include <sys/types.h> 47 #include <ctype.h> 48 #include "datatop_interface.h" 49 #include "datatop_fileops.h" 50 #include "datatop_str.h" 51 #include "datatop_polling.h" 52 53 #define DTOP_GEN_SIZE 8192 54 #define DTOP_GEN_LINE (DTOP_GEN_SIZE>>2) 55 #define NO_CPUS_ONLINE -1 56 57 /** 58 * @brief Searches /sys/devices/system/cpu/ directory to get find number of CPUs. 59 * 60 * @return Number of CPUs found in directory. 61 */ 62 static int dtop_cpu_search(void) 63 { 64 DIR *dp; 65 struct dirent *entry; 66 struct stat s; 67 int cpu_amt; 68 char cwd[1024]; 69 70 if (!getcwd(cwd, sizeof(cwd))) { 71 fprintf(stderr, "Failed to get current working dir\n"); 72 return -1; 73 } 74 75 dp = opendir("/sys/devices/system/cpu/"); 76 if (dp == NULL) { 77 fprintf(stderr, "err=%d: %s\n", errno, strerror(errno)); 78 fprintf(stderr, "Cannot open directory: %s\n", 79 "/sys/devices/system/cpu/"); 80 return NO_CPUS_ONLINE; 81 } 82 83 chdir("/sys/devices/system/cpu/"); 84 cpu_amt = 0; 85 while ((entry = readdir(dp))) { 86 if (stat(entry->d_name, &s)) { 87 printf("stat err=%d: %s\n", errno, strerror(errno)); 88 return NO_CPUS_ONLINE; 89 } 90 91 if (entry->d_name[0] == 'c' && 92 entry->d_name[1] == 'p' && 93 entry->d_name[2] == 'u' && 94 (isdigit(entry->d_name[3]))) { 95 96 cpu_amt++; 97 } 98 } 99 100 closedir(dp); 101 chdir(cwd); 102 return cpu_amt; 103 } 104 105 /** 106 * @brief Creates a dpg designed for CPU online and CPU scaling_cur_freq stats. 107 * 108 * @param name Name of file dpg represents. 109 */ 110 static void construct_cpu_stat_dpg(char *name) 111 { 112 char *file = malloc(strlen(name) + 1); 113 struct dtop_data_point *dp = 114 malloc(sizeof(struct dtop_data_point)); 115 struct dtop_data_point_gatherer *dpg = malloc 116 (sizeof(struct dtop_data_point_gatherer)); 117 118 strlcpy(file, name, strlen(name) + 1); 119 120 dp[0].type = DTOP_ULONG; 121 dp[0].name = malloc(5); 122 strlcpy(dp[0].name, "", 5); 123 dp[0].prefix = NULL; 124 dp[0].data.d_ulong = 0; 125 dp[0].initial_data.d_ulong = 0; 126 dp[0].skip = DO_NOT_SKIP; 127 dp[0].initial_data_populated = NOT_POPULATED; 128 129 dpg->prefix = file; 130 dpg->file = file; 131 dpg->poll = dtop_value_only_poll; 132 dpg->data_points = dp; 133 dpg->data_points_len = 1; 134 dpg->deconstruct = dtop_value_only_dpg_deconstructor; 135 136 dtop_register(dpg); 137 } 138 139 /** 140 * @brief Calls dpg constructor for necessary CPU stat files. 141 * 142 * Creates file names based on number of CPUs found and calls the 143 * dpg constructor for them. 144 * 145 * @param file Directory where the CPUs are found. 146 * @param add String which is concatenated onto file and represents 147 * the path after a CPU directory is entered. 148 * @param cpu_amt Amount of CPUs found on device. 149 */ 150 static void cpu_poll_helper(char *file, char *add, int cpu_amt) 151 { 152 int i; 153 for (i = 0; i < cpu_amt; i++) { 154 char *cpu_num = malloc(5); 155 char *newfile; 156 int nf_len; 157 snprintf(cpu_num, 5, "%d", i); 158 nf_len = strlen(file) + strlen(add) + strlen(cpu_num) + 2; 159 newfile = malloc(nf_len); 160 strlcpy(newfile, file, nf_len); 161 strlcat(newfile, cpu_num, nf_len); 162 strlcat(newfile, add, nf_len); 163 free(cpu_num); 164 construct_cpu_stat_dpg(newfile); 165 free(newfile); 166 } 167 } 168 169 /** 170 * @brief Calls necessary functions for CPU stat dpgs. 171 */ 172 void dtop_cpu_stats_init(void) 173 { 174 int cpu_amt; 175 char *file = "/sys/devices/system/cpu/cpu"; 176 char *add = "/cpufreq/scaling_cur_freq"; 177 178 cpu_amt = dtop_cpu_search(); 179 cpu_poll_helper(file, add, cpu_amt); 180 add = "/online"; 181 cpu_poll_helper(file, add, cpu_amt); 182 } 183