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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/*
* util.c
*
* Copyright (C) 2014 SUSE except where noted. All rights reserved.
*
* Code taken from btrfs-progs/util.c is:
* Copyright (C) 2007 Oracle. All rights reserved.
* Copyright (C) 2008 Morey Roof. All rights reserved.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* Authors: Mark Fasheh <mfasheh@suse.de>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <inttypes.h>
#ifdef __GLIBC__
#include <execinfo.h>
#endif
#include <sys/time.h>
#include <sys/types.h>
#include <regex.h>
#include <dirent.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include "debug.h"
#include "util.h"
int human_readable = 0;
uint64_t parse_size(char *s)
{
int i;
char c;
uint64_t mult = 1;
for (i = 0; s && s[i] && isdigit(s[i]); i++) ;
if (!i) {
fprintf(stderr, "ERROR: size value is empty\n");
exit(50);
}
if (s[i]) {
c = tolower(s[i]);
switch (c) {
case 'e':
mult *= 1024;
/* fallthrough */
case 'p':
mult *= 1024;
/* fallthrough */
case 't':
mult *= 1024;
/* fallthrough */
case 'g':
case 'G':
mult *= 1024;
/* fallthrough */
case 'm':
case 'M':
mult *= 1024;
/* fallthrough */
case 'k':
case 'K':
mult *= 1024;
/* fallthrough */
case 'b':
break;
default:
fprintf(stderr, "ERROR: Unknown size descriptor "
"'%c'\n", c);
exit(1);
}
}
if (s[i] && s[i+1]) {
fprintf(stderr, "ERROR: Illegal suffix contains "
"character '%c' in wrong position\n",
s[i+1]);
exit(51);
}
return strtoull(s, NULL, 10) * mult;
}
static char *size_strs[] = { "", "K", "M", "G", "T", "P", "E"};
int pretty_size_snprintf(uint64_t size, char *str, size_t str_bytes)
{
uint32_t num_divs = 0;
float fraction;
if (str_bytes == 0)
return 0;
if (!human_readable)
return snprintf(str, str_bytes, "%"PRIu64, size);
if (size < 1024){
fraction = size;
num_divs = 0;
} else {
uint64_t last_size = size;
num_divs = 0;
while(size >= 1024){
last_size = size;
size /= 1024;
num_divs ++;
}
if (num_divs >= ARRAY_SIZE(size_strs)) {
str[0] = '\0';
return -1;
}
fraction = (float)last_size / 1024;
}
return snprintf(str, str_bytes, "%.1f%s", fraction,
size_strs[num_divs]);
}
void print_stack_trace(void)
{
void *trace[16];
char **messages = (char **)NULL;
int i, trace_size = 0;
#ifdef __GLIBC__
trace_size = backtrace(trace, 16);
messages = backtrace_symbols(trace, trace_size);
printf("[stack trace follows]\n");
for (i=0; i < trace_size; i++)
printf("%s\n", messages[i]);
free(messages);
#endif
}
void record_start(struct elapsed_time *e, const char *name)
{
e->name = name;
gettimeofday(&e->start, NULL);
}
static void record_end(struct elapsed_time *e)
{
gettimeofday(&e->end, NULL);
e->elapsed = (e->end.tv_sec - e->start.tv_sec) +
((e->end.tv_usec - e->start.tv_usec) / 1000000.0F);
}
void record_end_print(struct elapsed_time *e)
{
record_end(e);
printf("%s took %fs\n", e->name, e->elapsed);
}
int num_digits(unsigned long long num)
{
unsigned int digits = 0;
while (num) {
num /= 10;
digits++;
}
return digits;
}
static int get_core_count_fallback(unsigned int *nr_phys, unsigned int *nr_log)
{
char path[PATH_MAX];
int ret = 0;
DIR *dirp;
regex_t regex;
regmatch_t pmatch[1];
size_t nmatch = 1;
struct dirent *entry;
int physical = 0 , logical = 0;
ret = snprintf(path, PATH_MAX, "/sys/devices/system/cpu/");
if (ret < 0)
return ret;
dirp = opendir(path);
if (dirp == NULL)
return errno;
ret = regcomp(®ex, "cpu[[:digit:]]+", REG_EXTENDED);
if (ret < 0)
goto out_freedir;
do {
entry = readdir(dirp);
if (entry) {
FILE *filp;
int sibling1, sibling2;
if (regexec(®ex, entry->d_name, nmatch, pmatch,
0) != 0)
continue;
#define FMT_STRING "/sys/devices/system/cpu/%s/topology/thread_siblings_list"
ret = snprintf(path, PATH_MAX, FMT_STRING,
entry->d_name);
if (ret < 0)
goto out;
// When HT is turned off hyperthreads won't have
// topology/ subdirectory
filp = fopen(path, "r");
if (filp == NULL)
continue;
physical++;
ret = fscanf(filp, "%d,%d", &sibling1, &sibling2);
logical += ret;
fclose(filp);
}
} while (entry != NULL);
// If HT is on logical/physical would have been counted twice due
// to the way /sys/devices/system/cpu/ is populated, in this case
// adjust counts.
if (logical > physical) {
logical /= 2;
physical /= 2;
}
*nr_log = logical;
*nr_phys = physical;
out:
regfree(®ex);
out_freedir:
closedir(dirp);
return ret;
}
int get_core_count(unsigned int *nr_phys, unsigned int *nr_log)
{
char *line = NULL;
size_t n = 0;;
int ret;
unsigned int logical = 0;
unsigned int physical = 0;
uint64_t sockets[64] = {0,}; /* supports up to 64 sockets with
64 cores per socket */
FILE *fp = popen("lscpu -p", "r");
if (fp == NULL) {
fprintf(stderr, "ERROR: Can't start lscpu\n");
return -EINVAL;
}
while ((ret = getline(&line, &n, fp) > 0)) {
unsigned int core, socket, unused;
/* Skip comment lines */
if (line[0] == '#')
continue;
logical++;
/* We only care about the core/socket id */
ret = sscanf(line, "%u,%u,%u", &unused, &core, &socket);
if (ret != 3) {
dprintf("Can't parse lscpu line: %s\n", line);
continue;
}
if (!(sockets[socket] & (1<<core))) {
physical++;
sockets[socket] |= (1<<core);
}
}
if (logical == 0 || physical == 0)
ret = -EINVAL;
else {
*nr_phys = physical;
*nr_log = logical;
}
free(line);
pclose(fp);
return ret;
}
void get_num_cpus(unsigned int *nr_phys, unsigned int *nr_log)
{
int ht = 0;
int ret = get_core_count(nr_phys, nr_log);
if (ret < 0) {
ret = get_core_count_fallback(nr_phys, nr_log);
if (ret < 0)
*nr_phys = *nr_log = sysconf(_SC_NPROCESSORS_ONLN);
} else
ht = *nr_log > *nr_phys;
dprintf("Detected %u logical and %u physical cpus (ht %s).\n",
*nr_log, *nr_phys, ht ? "is on" :
ret < 0 ? "detection broken" : "is off");
}
|