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