-
Notifications
You must be signed in to change notification settings - Fork 71
/
radeontop.c
160 lines (138 loc) · 4 KB
/
radeontop.c
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
/*
Copyright (C) 2012 Lauri Kasanen
Copyright (C) 2018 Genesis Cloud Ltd.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "radeontop.h"
#include <getopt.h>
void die(const char * const why) {
puts(why);
exit(1);
}
static void version() {
printf("RadeonTop %s\n", VERSION);
exit(1);
}
static void help(const char * const me, const unsigned int ticks, const unsigned int dumpinterval) {
printf(_("\n\tRadeonTop for R600 and above.\n\n"
"\tUsage: %s [-chmv] [-b bus] [-d file] [-i seconds] [-l limit] [-p device] [-t ticks]\n\n"
"-b --bus 3 Pick card from this PCI bus (hexadecimal)\n"
"-c --color Enable colors\n"
"-d --dump file Dump data to this file, - for stdout\n"
"-i --dump-interval 1 Number of seconds between dumps (default %u)\n"
"-l --limit 3 Quit after dumping N lines, default forever\n"
"-m --mem Force the /dev/mem path, for the proprietary driver\n"
"-p --path device Open DRM device node by path\n"
"-t --ticks 50 Samples per second (default %u)\n"
"-T --transparency Enable transparency\n"
"\n"
"-h --help Show this help\n"
"-v --version Show the version\n"),
me, dumpinterval, ticks);
die("");
}
int main(int argc, char **argv) {
// Temporarily drop privileges to do option parsing, etc.
seteuid(getuid());
const unsigned int default_ticks = 120;
const unsigned int default_dumpinterval = 1;
unsigned int ticks = default_ticks;
unsigned char color = 0;
unsigned char transparency = 0;
short bus = -1;
unsigned char forcemem = 0;
unsigned int device_id = 0;
unsigned int limit = 0;
char *dump = NULL;
unsigned int dumpinterval = default_dumpinterval;
const char *path = NULL;
// Translations
#ifdef ENABLE_NLS
setlocale(LC_ALL, "");
bindtextdomain("radeontop", "/usr/share/locale");
textdomain("radeontop");
#endif
// opts
const struct option opts[] = {
{"bus", 1, 0, 'b'},
{"color", 0, 0, 'c'},
{"dump", 1, 0, 'd'},
{"dump-interval", 1, 0, 'i'},
{"help", 0, 0, 'h'},
{"limit", 1, 0, 'l'},
{"mem", 0, 0, 'm'},
{"path", 1, 0, 'p'},
{"ticks", 1, 0, 't'},
{"transparency", 0, 0, 'T'},
{"version", 0, 0, 'v'},
{0, 0, 0, 0}
};
while (1) {
int c = getopt_long(argc, argv, "b:cTd:hi:l:mp:t:v", opts, NULL);
if (c == -1) break;
switch(c) {
case 'h':
case '?':
help(argv[0], default_ticks, default_dumpinterval);
break;
case 't':
ticks = atoi(optarg);
break;
case 'T':
transparency = 1;
break;
case 'c':
color = 1;
break;
case 'm':
forcemem = 1;
break;
case 'b':
bus = strtoul(optarg, NULL, 16);
break;
case 'v':
version();
break;
case 'l':
limit = atoi(optarg);
break;
case 'd':
dump = optarg;
break;
case 'i':
dumpinterval = atoi(optarg);
if (dumpinterval < 1)
dumpinterval = 1;
break;
case 'p':
path = optarg;
break;
}
}
// init (regain privileges for bus initialization and ultimately drop them afterwards)
seteuid(0);
init_pci(path, &bus, &device_id, forcemem);
// after init_pci we can assume that bus/device_id exists (otherwise it would die())
setuid(getuid());
const int family = getfamily(device_id);
if (!family)
puts(_("Unknown Radeon card. <= R500 won't work, new cards might."));
const char * const cardname = family_str[family];
initbits(family);
// runtime
collect(ticks, dumpinterval);
if (dump)
dumpdata(ticks, dump, limit, bus, dumpinterval);
else
present(ticks, cardname, color,transparency, bus, dumpinterval);
cleanup();
return 0;
}