m5.c (5754:b50a557f93df) m5.c (5755:8ef4ad572a6b)
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31#ifdef linux
32#define _GNU_SOURCE
33#include <sched.h>
34#endif
35
31#include <inttypes.h>
32#include <err.h>
33#include <fcntl.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38
39#include "m5op.h"
40
41char *progname;
42char *command = "unspecified";
43void usage();
44
45void
46parse_int_args(int argc, char *argv[], uint64_t ints[], int len)
47{
48 if (argc > len)
49 usage();
50
51 int i;
52 for (i = 0; i < len; ++i)
53 ints[i] = (i < argc) ? strtoul(argv[i], NULL, 0) : 0;
54}
55
56int
57read_file(int dest_fid)
58{
59 char buf[256*1024];
60 int offset = 0;
61 int len;
62
63 while ((len = m5_readfile(buf, sizeof(buf), offset)) > 0) {
64 write(dest_fid, buf, len);
65 offset += len;
66 }
67}
68
69void
70do_exit(int argc, char *argv[])
71{
72 if (argc > 1)
73 usage();
74
75 m5_exit((argc > 0) ? strtoul(argv[0], NULL, 0) : 0);
76}
77
78void
79do_reset_stats(int argc, char *argv[])
80{
81 uint64_t ints[2];
82 parse_int_args(argc, argv, ints, 2);
83 m5_reset_stats(ints[0], ints[1]);
84}
85
86void
87do_dump_stats(int argc, char *argv[])
88{
89 uint64_t ints[2];
90 parse_int_args(argc, argv, ints, 2);
91 m5_dump_stats(ints[0], ints[1]);
92}
93
94void
95do_dump_reset_stats(int argc, char *argv[])
96{
97 uint64_t ints[2];
98 parse_int_args(argc, argv, ints, 2);
99 m5_dumpreset_stats(ints[0], ints[1]);
100}
101
102void
103do_read_file(int argc, char *argv[])
104{
105 if (argc > 0)
106 usage();
107
108 read_file(STDOUT_FILENO);
109}
110
111void
112do_exec_file(int argc, char *argv[])
113{
114 if (argc > 0)
115 usage();
116
117 const char *destname = "/tmp/execfile";
118
119 int fid = open(destname, O_WRONLY, 0777);
120 int len = read_file(fid);
121 close(fid);
122 if (len > 0) {
123 execl(destname, "execfile", NULL);
124 err(1, "execl failed!");
125 }
126}
127
128void
129do_checkpoint(int argc, char *argv[])
130{
131 uint64_t ints[2];
132 parse_int_args(argc, argv, ints, 2);
133 m5_checkpoint(ints[0], ints[1]);
134}
135
136void
137do_load_symbol(int argc, char *argv[])
138{
139 if (argc != 2)
140 usage();
141
142 uint64_t addr = strtoul(argv[0], NULL, 0);
143 char *symbol = argv[1];
144 m5_loadsymbol(addr, symbol);
145}
146
147void
148do_initparam(int argc, char *argv[])
149{
150 if (argc != 0)
151 usage();
152
153
154 printf("%ld", m5_initparam());
155}
156
157void
158do_sw99param(int argc, char *argv[])
159{
160 if (argc != 0)
161 usage();
162
163 uint64_t param = m5_initparam();
164
165 // run-time, rampup-time, rampdown-time, warmup-time, connections
166 printf("%d %d %d %d %d", (param >> 48) & 0xfff,
167 (param >> 36) & 0xfff, (param >> 24) & 0xfff,
168 (param >> 12) & 0xfff, (param >> 0) & 0xfff);
169}
170
36#include <inttypes.h>
37#include <err.h>
38#include <fcntl.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44#include "m5op.h"
45
46char *progname;
47char *command = "unspecified";
48void usage();
49
50void
51parse_int_args(int argc, char *argv[], uint64_t ints[], int len)
52{
53 if (argc > len)
54 usage();
55
56 int i;
57 for (i = 0; i < len; ++i)
58 ints[i] = (i < argc) ? strtoul(argv[i], NULL, 0) : 0;
59}
60
61int
62read_file(int dest_fid)
63{
64 char buf[256*1024];
65 int offset = 0;
66 int len;
67
68 while ((len = m5_readfile(buf, sizeof(buf), offset)) > 0) {
69 write(dest_fid, buf, len);
70 offset += len;
71 }
72}
73
74void
75do_exit(int argc, char *argv[])
76{
77 if (argc > 1)
78 usage();
79
80 m5_exit((argc > 0) ? strtoul(argv[0], NULL, 0) : 0);
81}
82
83void
84do_reset_stats(int argc, char *argv[])
85{
86 uint64_t ints[2];
87 parse_int_args(argc, argv, ints, 2);
88 m5_reset_stats(ints[0], ints[1]);
89}
90
91void
92do_dump_stats(int argc, char *argv[])
93{
94 uint64_t ints[2];
95 parse_int_args(argc, argv, ints, 2);
96 m5_dump_stats(ints[0], ints[1]);
97}
98
99void
100do_dump_reset_stats(int argc, char *argv[])
101{
102 uint64_t ints[2];
103 parse_int_args(argc, argv, ints, 2);
104 m5_dumpreset_stats(ints[0], ints[1]);
105}
106
107void
108do_read_file(int argc, char *argv[])
109{
110 if (argc > 0)
111 usage();
112
113 read_file(STDOUT_FILENO);
114}
115
116void
117do_exec_file(int argc, char *argv[])
118{
119 if (argc > 0)
120 usage();
121
122 const char *destname = "/tmp/execfile";
123
124 int fid = open(destname, O_WRONLY, 0777);
125 int len = read_file(fid);
126 close(fid);
127 if (len > 0) {
128 execl(destname, "execfile", NULL);
129 err(1, "execl failed!");
130 }
131}
132
133void
134do_checkpoint(int argc, char *argv[])
135{
136 uint64_t ints[2];
137 parse_int_args(argc, argv, ints, 2);
138 m5_checkpoint(ints[0], ints[1]);
139}
140
141void
142do_load_symbol(int argc, char *argv[])
143{
144 if (argc != 2)
145 usage();
146
147 uint64_t addr = strtoul(argv[0], NULL, 0);
148 char *symbol = argv[1];
149 m5_loadsymbol(addr, symbol);
150}
151
152void
153do_initparam(int argc, char *argv[])
154{
155 if (argc != 0)
156 usage();
157
158
159 printf("%ld", m5_initparam());
160}
161
162void
163do_sw99param(int argc, char *argv[])
164{
165 if (argc != 0)
166 usage();
167
168 uint64_t param = m5_initparam();
169
170 // run-time, rampup-time, rampdown-time, warmup-time, connections
171 printf("%d %d %d %d %d", (param >> 48) & 0xfff,
172 (param >> 36) & 0xfff, (param >> 24) & 0xfff,
173 (param >> 12) & 0xfff, (param >> 0) & 0xfff);
174}
175
176#ifdef linux
177void
178do_pin(int argc, char *argv[])
179{
180 if (argc < 2)
181 usage();
182
183 cpu_set_t mask;
184 CPU_ZERO(&mask);
185
186 const char *sep = ",";
187 char *target = strtok(argv[0], sep);
188 while (target) {
189 CPU_SET(atoi(target), &mask);
190 target = strtok(NULL, sep);
191 }
192
193 if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) < 0)
194 err(1, "setaffinity");
195
196 execvp(argv[1], &argv[1]);
197 err(1, "execvp failed!");
198}
199#endif
200
171struct MainFunc
172{
173 char *name;
174 void (*func)(int argc, char *argv[]);
175 char *usage;
176};
177
178struct MainFunc mainfuncs[] = {
179 { "exit", do_exit, "[delay]" },
180 { "resetstats", do_reset_stats, "[delay [period]]" },
181 { "dumpstats", do_dump_stats, "[delay [period]]" },
182 { "dumpresetstats", do_dump_reset_stats, "[delay [period]]" },
183 { "readfile", do_read_file, "[filename]" },
184 { "execfile", do_exec_file, "<filename>" },
185 { "checkpoint", do_checkpoint, "[delay [period]]" },
186 { "loadsymbol", do_load_symbol, "<address> <symbol>" },
187 { "initparam", do_initparam, "" },
188 { "sw99param", do_sw99param, "" },
201struct MainFunc
202{
203 char *name;
204 void (*func)(int argc, char *argv[]);
205 char *usage;
206};
207
208struct MainFunc mainfuncs[] = {
209 { "exit", do_exit, "[delay]" },
210 { "resetstats", do_reset_stats, "[delay [period]]" },
211 { "dumpstats", do_dump_stats, "[delay [period]]" },
212 { "dumpresetstats", do_dump_reset_stats, "[delay [period]]" },
213 { "readfile", do_read_file, "[filename]" },
214 { "execfile", do_exec_file, "<filename>" },
215 { "checkpoint", do_checkpoint, "[delay [period]]" },
216 { "loadsymbol", do_load_symbol, "<address> <symbol>" },
217 { "initparam", do_initparam, "" },
218 { "sw99param", do_sw99param, "" },
219#ifdef linux
220 { "pin", do_pin, "<cpu> <program> [args ...]" }
221#endif
189};
190int numfuncs = sizeof(mainfuncs) / sizeof(mainfuncs[0]);
191
192void
193usage()
194{
195 int i;
196
197 for (i = 0; i < numfuncs; ++i) {
198 char *header = i ? "" : "usage:";
199 fprintf(stderr, "%-6s %s %s %s\n",
200 header, progname, mainfuncs[i].name, mainfuncs[i].usage);
201 }
202 fprintf(stderr, "\n");
203 fprintf(stderr, "All times in nanoseconds!\n");
204
205 exit(1);
206}
207
208int
209main(int argc, char *argv[])
210{
211 progname = argv[0];
212 if (argc < 2)
213 usage(1);
214
215 command = argv[1];
216
217 argv += 2;
218 argc -= 2;
219
220 int i;
221 for (i = 0; i < numfuncs; ++i) {
222 if (strcmp(command, mainfuncs[i].name) != 0)
223 continue;
224
225 mainfuncs[i].func(argc, argv);
226 exit(0);
227 }
228
229 usage(1);
230}
222};
223int numfuncs = sizeof(mainfuncs) / sizeof(mainfuncs[0]);
224
225void
226usage()
227{
228 int i;
229
230 for (i = 0; i < numfuncs; ++i) {
231 char *header = i ? "" : "usage:";
232 fprintf(stderr, "%-6s %s %s %s\n",
233 header, progname, mainfuncs[i].name, mainfuncs[i].usage);
234 }
235 fprintf(stderr, "\n");
236 fprintf(stderr, "All times in nanoseconds!\n");
237
238 exit(1);
239}
240
241int
242main(int argc, char *argv[])
243{
244 progname = argv[0];
245 if (argc < 2)
246 usage(1);
247
248 command = argv[1];
249
250 argv += 2;
251 argc -= 2;
252
253 int i;
254 for (i = 0; i < numfuncs; ++i) {
255 if (strcmp(command, mainfuncs[i].name) != 0)
256 continue;
257
258 mainfuncs[i].func(argc, argv);
259 exit(0);
260 }
261
262 usage(1);
263}