Skip to content

Commit 6953beb

Browse files
olsajiriacmel
authored andcommitted
perf clockid: Move parse_clockid() to new clockid object
Move parse_clockid and all needed clcckid related stuff into clockid object. We are going to add clockid_name function in following change, so it's better it's placed in separated object and not in builtin-record.c. No functional change is intended. Signed-off-by: Jiri Olsa <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Andi Kleen <[email protected]> Cc: David Ahern <[email protected]> Cc: Geneviève Bastien <[email protected]> Cc: Ian Rogers <[email protected]> Cc: Jeremie Galarneau <[email protected]> Cc: Michael Petlan <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Wang Nan <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 7d65864 commit 6953beb

File tree

4 files changed

+119
-97
lines changed

4 files changed

+119
-97
lines changed

tools/perf/builtin-record.c

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "util/bpf-event.h"
4747
#include "util/util.h"
4848
#include "util/pfm.h"
49+
#include "util/clockid.h"
4950
#include "asm/bug.h"
5051
#include "perf.h"
5152

@@ -2102,103 +2103,6 @@ static int perf_record_config(const char *var, const char *value, void *cb)
21022103
return 0;
21032104
}
21042105

2105-
struct clockid_map {
2106-
const char *name;
2107-
int clockid;
2108-
};
2109-
2110-
#define CLOCKID_MAP(n, c) \
2111-
{ .name = n, .clockid = (c), }
2112-
2113-
#define CLOCKID_END { .name = NULL, }
2114-
2115-
2116-
/*
2117-
* Add the missing ones, we need to build on many distros...
2118-
*/
2119-
#ifndef CLOCK_MONOTONIC_RAW
2120-
#define CLOCK_MONOTONIC_RAW 4
2121-
#endif
2122-
#ifndef CLOCK_BOOTTIME
2123-
#define CLOCK_BOOTTIME 7
2124-
#endif
2125-
#ifndef CLOCK_TAI
2126-
#define CLOCK_TAI 11
2127-
#endif
2128-
2129-
static const struct clockid_map clockids[] = {
2130-
/* available for all events, NMI safe */
2131-
CLOCKID_MAP("monotonic", CLOCK_MONOTONIC),
2132-
CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW),
2133-
2134-
/* available for some events */
2135-
CLOCKID_MAP("realtime", CLOCK_REALTIME),
2136-
CLOCKID_MAP("boottime", CLOCK_BOOTTIME),
2137-
CLOCKID_MAP("tai", CLOCK_TAI),
2138-
2139-
/* available for the lazy */
2140-
CLOCKID_MAP("mono", CLOCK_MONOTONIC),
2141-
CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW),
2142-
CLOCKID_MAP("real", CLOCK_REALTIME),
2143-
CLOCKID_MAP("boot", CLOCK_BOOTTIME),
2144-
2145-
CLOCKID_END,
2146-
};
2147-
2148-
static int get_clockid_res(clockid_t clk_id, u64 *res_ns)
2149-
{
2150-
struct timespec res;
2151-
2152-
*res_ns = 0;
2153-
if (!clock_getres(clk_id, &res))
2154-
*res_ns = res.tv_nsec + res.tv_sec * NSEC_PER_SEC;
2155-
else
2156-
pr_warning("WARNING: Failed to determine specified clock resolution.\n");
2157-
2158-
return 0;
2159-
}
2160-
2161-
static int parse_clockid(const struct option *opt, const char *str, int unset)
2162-
{
2163-
struct record_opts *opts = (struct record_opts *)opt->value;
2164-
const struct clockid_map *cm;
2165-
const char *ostr = str;
2166-
2167-
if (unset) {
2168-
opts->use_clockid = 0;
2169-
return 0;
2170-
}
2171-
2172-
/* no arg passed */
2173-
if (!str)
2174-
return 0;
2175-
2176-
/* no setting it twice */
2177-
if (opts->use_clockid)
2178-
return -1;
2179-
2180-
opts->use_clockid = true;
2181-
2182-
/* if its a number, we're done */
2183-
if (sscanf(str, "%d", &opts->clockid) == 1)
2184-
return get_clockid_res(opts->clockid, &opts->clockid_res_ns);
2185-
2186-
/* allow a "CLOCK_" prefix to the name */
2187-
if (!strncasecmp(str, "CLOCK_", 6))
2188-
str += 6;
2189-
2190-
for (cm = clockids; cm->name; cm++) {
2191-
if (!strcasecmp(str, cm->name)) {
2192-
opts->clockid = cm->clockid;
2193-
return get_clockid_res(opts->clockid,
2194-
&opts->clockid_res_ns);
2195-
}
2196-
}
2197-
2198-
opts->use_clockid = false;
2199-
ui__warning("unknown clockid %s, check man page\n", ostr);
2200-
return -1;
2201-
}
22022106

22032107
static int record__parse_affinity(const struct option *opt, const char *str, int unset)
22042108
{

tools/perf/util/Build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ perf-y += expr-bison.o
128128
perf-y += expr.o
129129
perf-y += branch.o
130130
perf-y += mem2node.o
131+
perf-y += clockid.o
131132

132133
perf-$(CONFIG_LIBBPF) += bpf-loader.o
133134
perf-$(CONFIG_LIBBPF) += bpf_map.o

tools/perf/util/clockid.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <subcmd/parse-options.h>
4+
#include <stdio.h>
5+
#include <time.h>
6+
#include <strings.h>
7+
#include <linux/time64.h>
8+
#include "debug.h"
9+
#include "clockid.h"
10+
#include "record.h"
11+
12+
struct clockid_map {
13+
const char *name;
14+
int clockid;
15+
};
16+
17+
#define CLOCKID_MAP(n, c) \
18+
{ .name = n, .clockid = (c), }
19+
20+
#define CLOCKID_END { .name = NULL, }
21+
22+
23+
/*
24+
* Add the missing ones, we need to build on many distros...
25+
*/
26+
#ifndef CLOCK_MONOTONIC_RAW
27+
#define CLOCK_MONOTONIC_RAW 4
28+
#endif
29+
#ifndef CLOCK_BOOTTIME
30+
#define CLOCK_BOOTTIME 7
31+
#endif
32+
#ifndef CLOCK_TAI
33+
#define CLOCK_TAI 11
34+
#endif
35+
36+
static const struct clockid_map clockids[] = {
37+
/* available for all events, NMI safe */
38+
CLOCKID_MAP("monotonic", CLOCK_MONOTONIC),
39+
CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW),
40+
41+
/* available for some events */
42+
CLOCKID_MAP("realtime", CLOCK_REALTIME),
43+
CLOCKID_MAP("boottime", CLOCK_BOOTTIME),
44+
CLOCKID_MAP("tai", CLOCK_TAI),
45+
46+
/* available for the lazy */
47+
CLOCKID_MAP("mono", CLOCK_MONOTONIC),
48+
CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW),
49+
CLOCKID_MAP("real", CLOCK_REALTIME),
50+
CLOCKID_MAP("boot", CLOCK_BOOTTIME),
51+
52+
CLOCKID_END,
53+
};
54+
55+
static int get_clockid_res(clockid_t clk_id, u64 *res_ns)
56+
{
57+
struct timespec res;
58+
59+
*res_ns = 0;
60+
if (!clock_getres(clk_id, &res))
61+
*res_ns = res.tv_nsec + res.tv_sec * NSEC_PER_SEC;
62+
else
63+
pr_warning("WARNING: Failed to determine specified clock resolution.\n");
64+
65+
return 0;
66+
}
67+
68+
int parse_clockid(const struct option *opt, const char *str, int unset)
69+
{
70+
struct record_opts *opts = (struct record_opts *)opt->value;
71+
const struct clockid_map *cm;
72+
const char *ostr = str;
73+
74+
if (unset) {
75+
opts->use_clockid = 0;
76+
return 0;
77+
}
78+
79+
/* no arg passed */
80+
if (!str)
81+
return 0;
82+
83+
/* no setting it twice */
84+
if (opts->use_clockid)
85+
return -1;
86+
87+
opts->use_clockid = true;
88+
89+
/* if its a number, we're done */
90+
if (sscanf(str, "%d", &opts->clockid) == 1)
91+
return get_clockid_res(opts->clockid, &opts->clockid_res_ns);
92+
93+
/* allow a "CLOCK_" prefix to the name */
94+
if (!strncasecmp(str, "CLOCK_", 6))
95+
str += 6;
96+
97+
for (cm = clockids; cm->name; cm++) {
98+
if (!strcasecmp(str, cm->name)) {
99+
opts->clockid = cm->clockid;
100+
return get_clockid_res(opts->clockid,
101+
&opts->clockid_res_ns);
102+
}
103+
}
104+
105+
opts->use_clockid = false;
106+
ui__warning("unknown clockid %s, check man page\n", ostr);
107+
return -1;
108+
}

tools/perf/util/clockid.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#ifndef __PERF_CLOCKID_H
4+
#define __PERF_CLOCKID_H
5+
6+
struct option;
7+
int parse_clockid(const struct option *opt, const char *str, int unset);
8+
9+
#endif

0 commit comments

Comments
 (0)