util.c 5.54 KB
Newer Older
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
/*
 * 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>
27
#ifdef __GLIBC__
28
#include <execinfo.h>
29
#endif
30
#include <sys/time.h>
31 32 33
#include <unistd.h>
#include <limits.h>
#include <errno.h>
34

35
#include "debug.h"
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
#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':
65
		case 'G':
66 67 68
			mult *= 1024;
			/* fallthrough */
		case 'm':
69
		case 'M':
70 71 72
			mult *= 1024;
			/* fallthrough */
		case 'k':
73
		case 'K':
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
			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)
{
96
	uint32_t num_divs = 0;
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
	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]);
}
126

127 128 129 130 131 132
void print_stack_trace(void)
{
	void *trace[16];
	char **messages = (char **)NULL;
	int i, trace_size = 0;

133
#ifdef __GLIBC__
134 135 136 137 138 139
	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);
140
#endif
141
}
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

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);
}
162 163 164 165 166 167 168 169 170 171 172

int num_digits(unsigned long long num)
{
	unsigned int digits = 0;

	while (num) {
		num /= 10;
		digits++;
	}
	return digits;
}
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

#define	VENDOR_KEY	"vendor_id"
#define	VENDOR_VAL	"GenuineIntel"
#define	FLAGS_KEY	"flags"
#define	HT_FLAG		"ht"
#define	CPUINFO_DELIM	':'
#define	FLAGS_DELIM	' '

/* Checks /proc/cpuinfo for an Intel CPU with hyperthreading. */
static int detect_ht(void)
{
	FILE *fp;
	int err = 0;
	int ret = 0;
	char line[LINE_MAX + 1];
	char *c, *val, *key, *flag;
	int check_vendor = 1;

	fp = fopen("/proc/cpuinfo", "r");
	if (fp == NULL) {
		err = errno;
		goto out;
	}

	while (fgets(line, LINE_MAX + 1, fp) != NULL) {
		c = strchr(line, CPUINFO_DELIM);
		if (!c)
			continue;
		key = line;
		val = c + 1;/* line is \0 delimited so this should be safe */

		if (*val == '\0') /* No value... */
			continue;

		/* Strip trailing whitespace from the key. */
		do {
			*c = '\0';
			c--;
		} while (c > key && !isalnum(*c));

		/* Strip leading and trailing whitespace from val. */
		while (isspace(*val) && *val != '\0')
			val++;
216 217
		if (*val == '\0')
			continue;
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
		c = &val[strlen(val) - 1];
		while (isspace(*c) && c >= val) {
			*c = '\0';
			c--;
		}
//		printf("key: \"%s\" val: \"%s\"\n", key, val);

		if (check_vendor && !strcmp(key, VENDOR_KEY)) {
			/* No intel == no ht */
			if (strcmp(val, VENDOR_VAL))
				goto out_close;
			check_vendor = 0;
		} else if (!strcmp(key, FLAGS_KEY)) {
			for (flag = val; flag && *flag; flag = c) {
				c = strchr(flag, FLAGS_DELIM);
				if (c) {
					*c = '\0';
235 236 237 238
					if (c < &line[LINE_MAX])
						c++;
					else
						c = NULL;
239 240
				}
//				printf("\"flag: %s\"\n", flag);
241 242 243 244
				if (!strcmp(flag, HT_FLAG)) {
					ret = 1;
					goto out_close;
				}
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
			}
			/* No 'ht' in flags? We're done. */
			goto out_close;
		}
	}
	if (ferror(fp))
		err = errno;

out_close:
	fclose(fp);
out:
	if (err)
		fprintf(stderr,
			"Error %d (\"%s\") while checking /proc/cpuinfo for "
			"hyperthreading -- will assume ht is off.\n", err,
			strerror(err));
	return ret;
}

void get_num_cpus(unsigned int *nr_phys, unsigned int *nr_log)
{
	int ht;

	*nr_phys = *nr_log = sysconf(_SC_NPROCESSORS_ONLN);
	ht = detect_ht();
	if (ht && *nr_phys >= 2)
		*nr_phys /= 2;

	dprintf("Detected %u logical and %u physical cpus (ht is %s).\n",
		*nr_log, *nr_phys, ht ? "on" : "off");
}