1 #include <stdlib.h> 2 #include <string.h> 3 #include <sys/time.h> 4 #include <linux/time64.h> 5 #include <time.h> 6 #include <errno.h> 7 #include <inttypes.h> 8 9 #include "perf.h" 10 #include "debug.h" 11 #include "time-utils.h" 12 13 int parse_nsec_time(const char *str, u64 *ptime) 14 { 15 u64 time_sec, time_nsec; 16 char *end; 17 18 time_sec = strtoul(str, &end, 10); 19 if (*end != '.' && *end != '\0') 20 return -1; 21 22 if (*end == '.') { 23 int i; 24 char nsec_buf[10]; 25 26 if (strlen(++end) > 9) 27 return -1; 28 29 strncpy(nsec_buf, end, 9); 30 nsec_buf[9] = '\0'; 31 32 /* make it nsec precision */ 33 for (i = strlen(nsec_buf); i < 9; i++) 34 nsec_buf[i] = '0'; 35 36 time_nsec = strtoul(nsec_buf, &end, 10); 37 if (*end != '\0') 38 return -1; 39 } else 40 time_nsec = 0; 41 42 *ptime = time_sec * NSEC_PER_SEC + time_nsec; 43 return 0; 44 } 45 46 static int parse_timestr_sec_nsec(struct perf_time_interval *ptime, 47 char *start_str, char *end_str) 48 { 49 if (start_str && (*start_str != '\0') && 50 (parse_nsec_time(start_str, &ptime->start) != 0)) { 51 return -1; 52 } 53 54 if (end_str && (*end_str != '\0') && 55 (parse_nsec_time(end_str, &ptime->end) != 0)) { 56 return -1; 57 } 58 59 return 0; 60 } 61 62 int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr) 63 { 64 char *start_str, *end_str; 65 char *d, *str; 66 int rc = 0; 67 68 if (ostr == NULL || *ostr == '\0') 69 return 0; 70 71 /* copy original string because we need to modify it */ 72 str = strdup(ostr); 73 if (str == NULL) 74 return -ENOMEM; 75 76 ptime->start = 0; 77 ptime->end = 0; 78 79 /* str has the format: <start>,<stop> 80 * variations: <start>, 81 * ,<stop> 82 * , 83 */ 84 start_str = str; 85 d = strchr(start_str, ','); 86 if (d) { 87 *d = '\0'; 88 ++d; 89 } 90 end_str = d; 91 92 rc = parse_timestr_sec_nsec(ptime, start_str, end_str); 93 94 free(str); 95 96 /* make sure end time is after start time if it was given */ 97 if (rc == 0 && ptime->end && ptime->end < ptime->start) 98 return -EINVAL; 99 100 pr_debug("start time %" PRIu64 ", ", ptime->start); 101 pr_debug("end time %" PRIu64 "\n", ptime->end); 102 103 return rc; 104 } 105 106 bool perf_time__skip_sample(struct perf_time_interval *ptime, u64 timestamp) 107 { 108 /* if time is not set don't drop sample */ 109 if (timestamp == 0) 110 return false; 111 112 /* otherwise compare sample time to time window */ 113 if ((ptime->start && timestamp < ptime->start) || 114 (ptime->end && timestamp > ptime->end)) { 115 return true; 116 } 117 118 return false; 119 } 120 121 int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz) 122 { 123 u64 sec = timestamp / NSEC_PER_SEC; 124 u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC; 125 126 return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec); 127 } 128 129 int fetch_current_timestamp(char *buf, size_t sz) 130 { 131 struct timeval tv; 132 struct tm tm; 133 char dt[32]; 134 135 if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm)) 136 return -1; 137 138 if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm)) 139 return -1; 140 141 scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000); 142 143 return 0; 144 } 145