Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/time64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <math.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "time-utils.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "session.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "evlist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) int parse_nsec_time(const char *str, u64 *ptime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	u64 time_sec, time_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	char *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	time_sec = strtoul(str, &end, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	if (*end != '.' && *end != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	if (*end == '.') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		char nsec_buf[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		if (strlen(++end) > 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		strncpy(nsec_buf, end, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		nsec_buf[9] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		/* make it nsec precision */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		for (i = strlen(nsec_buf); i < 9; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 			nsec_buf[i] = '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		time_nsec = strtoul(nsec_buf, &end, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		if (*end != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		time_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	*ptime = time_sec * NSEC_PER_SEC + time_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static int parse_timestr_sec_nsec(struct perf_time_interval *ptime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 				  char *start_str, char *end_str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (start_str && (*start_str != '\0') &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	    (parse_nsec_time(start_str, &ptime->start) != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (end_str && (*end_str != '\0') &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	    (parse_nsec_time(end_str, &ptime->end) != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static int split_start_end(char **start, char **end, const char *ostr, char ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	char *start_str, *end_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	char *d, *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (ostr == NULL || *ostr == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	/* copy original string because we need to modify it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	str = strdup(ostr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (str == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	start_str = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	d = strchr(start_str, ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (d) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		*d = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		++d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	end_str = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	*start = start_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	*end = end_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	char *start_str = NULL, *end_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	rc = split_start_end(&start_str, &end_str, ostr, ',');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (rc || !start_str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	ptime->start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	ptime->end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	rc = parse_timestr_sec_nsec(ptime, start_str, end_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	free(start_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	/* make sure end time is after start time if it was given */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (rc == 0 && ptime->end && ptime->end < ptime->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	pr_debug("start time %" PRIu64 ", ", ptime->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	pr_debug("end time %" PRIu64 "\n", ptime->end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static int perf_time__parse_strs(struct perf_time_interval *ptime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 				 const char *ostr, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	const char *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	char *str, *arg, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	int i, num = 0, rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	/* Count the commas */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	for (cp = ostr; *cp; cp++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		num += !!(*cp == ',');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (!num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	BUG_ON(num > size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	str = strdup(ostr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (!str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	/* Split the string and parse each piece, except the last */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	for (i = 0, p = str; i < num - 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		arg = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		/* Find next comma, there must be one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		p = skip_spaces(strchr(p, ',') + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		/* Skip the value, must not contain space or comma */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		while (*p && !isspace(*p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			if (*p++ == ',') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 				rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		/* Split and parse */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		if (*p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			*p++ = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		rc = perf_time__parse_str(ptime + i, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	/* Parse the last piece */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	rc = perf_time__parse_str(ptime + i, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	/* Check there is no overlap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	for (i = 0; i < num - 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		if (ptime[i].end >= ptime[i + 1].start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	rc = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	free(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static int parse_percent(double *pcnt, char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	char *c, *endptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	double d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	c = strchr(str, '%');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		*c = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	d = strtod(str, &endptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (endptr != str + strlen(str))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	*pcnt = d / 100.0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static int set_percent_time(struct perf_time_interval *ptime, double start_pcnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			    double end_pcnt, u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	u64 total = end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if (start_pcnt < 0.0 || start_pcnt > 1.0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	    end_pcnt < 0.0 || end_pcnt > 1.0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	ptime->start = start + round(start_pcnt * total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	ptime->end = start + round(end_pcnt * total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (ptime->end > ptime->start && ptime->end != end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		ptime->end -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static int percent_slash_split(char *str, struct perf_time_interval *ptime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			       u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	char *p, *end_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	double pcnt, start_pcnt, end_pcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	 * Example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	 * 10%/2: select the second 10% slice and the third 10% slice
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	/* We can modify this string since the original one is copied */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	p = strchr(str, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	*p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (parse_percent(&pcnt, str) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	i = (int)strtol(p, &end_str, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	if (*end_str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (pcnt <= 0.0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	start_pcnt = pcnt * (i - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	end_pcnt = pcnt * i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	return set_percent_time(ptime, start_pcnt, end_pcnt, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static int percent_dash_split(char *str, struct perf_time_interval *ptime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			      u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	char *start_str = NULL, *end_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	double start_pcnt, end_pcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	 * Example: 0%-10%
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	ret = split_start_end(&start_str, &end_str, str, '-');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (ret || !start_str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if ((parse_percent(&start_pcnt, start_str) != 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	    (parse_percent(&end_pcnt, end_str) != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		free(start_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	free(start_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	return set_percent_time(ptime, start_pcnt, end_pcnt, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) typedef int (*time_pecent_split)(char *, struct perf_time_interval *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 				 u64 start, u64 end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static int percent_comma_split(struct perf_time_interval *ptime_buf, int num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			       const char *ostr, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			       time_pecent_split func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	char *str, *p1, *p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	int len, ret, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	str = strdup(ostr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (str == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	len = strlen(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	p1 = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	while (p1 < str + len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		if (i >= num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			free(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		p2 = strchr(p1, ',');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		if (p2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			*p2 = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		ret = (func)(p1, &ptime_buf[i], start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			free(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		pr_debug("start time %d: %" PRIu64 ", ", i, ptime_buf[i].start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		pr_debug("end time %d: %" PRIu64 "\n", i, ptime_buf[i].end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		if (p2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			p1 = p2 + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	free(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int one_percent_convert(struct perf_time_interval *ptime_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			       const char *ostr, u64 start, u64 end, char *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	int len = strlen(ostr), ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	 * c points to '%'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	 * '%' should be the last character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	if (ostr + len - 1 != c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	 * Construct a string like "xx%/1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	str = malloc(len + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (str == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	memcpy(str, ostr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	strcpy(str + len, "/1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	ret = percent_slash_split(str, ptime_buf, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	free(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) int perf_time__percent_parse_str(struct perf_time_interval *ptime_buf, int num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 				 const char *ostr, u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	char *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	 * ostr example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	 * 10%/2,10%/3: select the second 10% slice and the third 10% slice
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	 * 0%-10%,30%-40%: multiple time range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	 * 50%: just one percent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	memset(ptime_buf, 0, sizeof(*ptime_buf) * num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	c = strchr(ostr, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	if (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		return percent_comma_split(ptime_buf, num, ostr, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 					   end, percent_slash_split);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	c = strchr(ostr, '-');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	if (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		return percent_comma_split(ptime_buf, num, ostr, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 					   end, percent_dash_split);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	c = strchr(ostr, '%');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	if (c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		return one_percent_convert(ptime_buf, ostr, start, end, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) struct perf_time_interval *perf_time__range_alloc(const char *ostr, int *size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	const char *p1, *p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	int i = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	struct perf_time_interval *ptime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	 * At least allocate one time range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	if (!ostr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		goto alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	p1 = ostr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	while (p1 < ostr + strlen(ostr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		p2 = strchr(p1, ',');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		if (!p2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		p1 = p2 + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	*size = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	ptime = calloc(i, sizeof(*ptime));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	return ptime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) bool perf_time__skip_sample(struct perf_time_interval *ptime, u64 timestamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	/* if time is not set don't drop sample */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	if (timestamp == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	/* otherwise compare sample time to time window */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	if ((ptime->start && timestamp < ptime->start) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	    (ptime->end && timestamp > ptime->end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) bool perf_time__ranges_skip_sample(struct perf_time_interval *ptime_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 				   int num, u64 timestamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	struct perf_time_interval *ptime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	if ((!ptime_buf) || (timestamp == 0) || (num == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (num == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		return perf_time__skip_sample(&ptime_buf[0], timestamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	 * start/end of multiple time ranges must be valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	for (i = 0; i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		ptime = &ptime_buf[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		if (timestamp >= ptime->start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		    (timestamp <= ptime->end || !ptime->end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) int perf_time__parse_for_ranges_reltime(const char *time_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 				struct perf_session *session,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 				struct perf_time_interval **ranges,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 				int *range_size, int *range_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 				bool reltime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	bool has_percent = strchr(time_str, '%');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	struct perf_time_interval *ptime_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	int size, num, ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	ptime_range = perf_time__range_alloc(time_str, &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	if (!ptime_range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	if (has_percent || reltime) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		if (session->evlist->first_sample_time == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		    session->evlist->last_sample_time == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 			pr_err("HINT: no first/last sample time found in perf data.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 			       "Please use latest perf binary to execute 'perf record'\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 			       "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	if (has_percent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		num = perf_time__percent_parse_str(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 				ptime_range, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 				time_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 				session->evlist->first_sample_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 				session->evlist->last_sample_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		num = perf_time__parse_strs(ptime_range, time_str, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	if (num < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		goto error_invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	if (reltime) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		for (i = 0; i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			ptime_range[i].start += session->evlist->first_sample_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 			ptime_range[i].end += session->evlist->first_sample_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	*range_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	*range_num = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	*ranges = ptime_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) error_invalid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	pr_err("Invalid time string\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	free(ptime_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) int perf_time__parse_for_ranges(const char *time_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 				struct perf_session *session,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 				struct perf_time_interval **ranges,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 				int *range_size, int *range_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	return perf_time__parse_for_ranges_reltime(time_str, session, ranges,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 					range_size, range_num, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	u64  sec = timestamp / NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) int timestamp__scnprintf_nsec(u64 timestamp, char *buf, size_t sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	u64 sec  = timestamp / NSEC_PER_SEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	    nsec = timestamp % NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	return scnprintf(buf, sz, "%" PRIu64 ".%09" PRIu64, sec, nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) int fetch_current_timestamp(char *buf, size_t sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	struct timeval tv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	struct tm tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	char dt[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }