m5.c revision 9451:f56816facd25
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// On 32 bit platforms we need to use strtoull to do the conversion 69#ifdef __LP64__ 70#define strto64 strtoul 71#else 72#define strto64 strtoull 73#endif 74 int i; 75 for (i = 0; i < len; ++i) 76 ints[i] = (i < argc) ? strto64(argv[i], NULL, 0) : 0; 77 78#undef strto64 79} 80 81int 82read_file(int dest_fid) 83{ 84 char buf[256*1024]; 85 int offset = 0; 86 int len; 87 88 // Touch all buffer pages to ensure they are mapped in the 89 // page table. This is required in the case of X86_FS, where 90 // Linux does demand paging. 91 memset(buf, 0, sizeof(buf)); 92 93 while ((len = m5_readfile(buf, sizeof(buf), offset)) > 0) { 94 write(dest_fid, buf, len); 95 offset += len; 96 } 97} 98 99int 100write_file(const char *filename) 101{ 102 fprintf(stderr, "opening %s\n", filename); 103 int src_fid = open(filename, O_RDONLY); 104 105 if (src_fid < 0) { 106 fprintf(stderr, "error opening %s\n", filename); 107 return; 108 } 109 110 char buf[256*1024]; 111 int offset = 0; 112 int len; 113 int bytes = 0; 114 115 memset(buf, 0, sizeof(buf)); 116 117 while ((len = read(src_fid, buf, sizeof(buf))) > 0) { 118 bytes += m5_writefile(buf, len, offset, filename); 119 offset += len; 120 } 121 fprintf(stderr, "written %d bytes\n", bytes); 122 123 close(src_fid); 124} 125 126void 127do_exit(int argc, char *argv[]) 128{ 129 if (argc > 1) 130 usage(); 131 132 uint64_t ints[1]; 133 parse_int_args(argc, argv, ints, 1); 134 m5_exit(ints[0]); 135} 136 137void 138do_reset_stats(int argc, char *argv[]) 139{ 140 uint64_t ints[2]; 141 parse_int_args(argc, argv, ints, 2); 142 m5_reset_stats(ints[0], ints[1]); 143} 144 145void 146do_dump_stats(int argc, char *argv[]) 147{ 148 uint64_t ints[2]; 149 parse_int_args(argc, argv, ints, 2); 150 m5_dump_stats(ints[0], ints[1]); 151} 152 153void 154do_dump_reset_stats(int argc, char *argv[]) 155{ 156 uint64_t ints[2]; 157 parse_int_args(argc, argv, ints, 2); 158 m5_dumpreset_stats(ints[0], ints[1]); 159} 160 161void 162do_read_file(int argc, char *argv[]) 163{ 164 if (argc > 0) 165 usage(); 166 167 read_file(STDOUT_FILENO); 168} 169 170void 171do_write_file(int argc, char *argv[]) 172{ 173 if (argc != 1) 174 usage(); 175 176 const char *filename = argv[0]; 177 178 write_file(filename); 179} 180 181void 182do_exec_file(int argc, char *argv[]) 183{ 184 if (argc > 0) 185 usage(); 186 187 const char *destname = "/tmp/execfile"; 188 189 int fid = open(destname, O_WRONLY, 0777); 190 int len = read_file(fid); 191 close(fid); 192 if (len > 0) { 193 execl(destname, "execfile", NULL); 194 err(1, "execl failed!"); 195 } 196} 197 198void 199do_checkpoint(int argc, char *argv[]) 200{ 201 uint64_t ints[2]; 202 parse_int_args(argc, argv, ints, 2); 203 m5_checkpoint(ints[0], ints[1]); 204} 205 206void 207do_load_symbol(int argc, char *argv[]) 208{ 209 if (argc != 2) 210 usage(); 211 212 uint64_t addr = strtoul(argv[0], NULL, 0); 213 char *symbol = argv[1]; 214 m5_loadsymbol(addr, symbol); 215} 216 217void 218do_initparam(int argc, char *argv[]) 219{ 220 if (argc != 0) 221 usage(); 222 223 uint64_t val = m5_initparam(); 224 printf("%"PRIu64, val); 225} 226 227void 228do_sw99param(int argc, char *argv[]) 229{ 230 if (argc != 0) 231 usage(); 232 233 uint64_t param = m5_initparam(); 234 235 // run-time, rampup-time, rampdown-time, warmup-time, connections 236 printf("%"PRId64" %"PRId64" %"PRId64" %"PRId64" %"PRId64, 237 (param >> 48) & 0xfff, 238 (param >> 36) & 0xfff, (param >> 24) & 0xfff, 239 (param >> 12) & 0xfff, (param >> 0) & 0xfff); 240} 241 242#ifdef linux 243void 244do_pin(int argc, char *argv[]) 245{ 246 if (argc < 2) 247 usage(); 248 249 cpu_set_t mask; 250 CPU_ZERO(&mask); 251 252 const char *sep = ","; 253 char *target = strtok(argv[0], sep); 254 while (target) { 255 CPU_SET(atoi(target), &mask); 256 target = strtok(NULL, sep); 257 } 258 259 if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) < 0) 260 err(1, "setaffinity"); 261 262 execvp(argv[1], &argv[1]); 263 err(1, "execvp failed!"); 264} 265#endif 266 267struct MainFunc 268{ 269 char *name; 270 void (*func)(int argc, char *argv[]); 271 char *usage; 272}; 273 274struct MainFunc mainfuncs[] = { 275 { "exit", do_exit, "[delay]" }, 276 { "resetstats", do_reset_stats, "[delay [period]]" }, 277 { "dumpstats", do_dump_stats, "[delay [period]]" }, 278 { "dumpresetstats", do_dump_reset_stats, "[delay [period]]" }, 279 { "readfile", do_read_file, "" }, 280 { "writefile", do_write_file, "<filename>" }, 281 { "execfile", do_exec_file, "" }, 282 { "checkpoint", do_checkpoint, "[delay [period]]" }, 283 { "loadsymbol", do_load_symbol, "<address> <symbol>" }, 284 { "initparam", do_initparam, "" }, 285 { "sw99param", do_sw99param, "" }, 286#ifdef linux 287 { "pin", do_pin, "<cpu> <program> [args ...]" } 288#endif 289}; 290int numfuncs = sizeof(mainfuncs) / sizeof(mainfuncs[0]); 291 292void 293usage() 294{ 295 int i; 296 297 for (i = 0; i < numfuncs; ++i) { 298 char *header = i ? "" : "usage:"; 299 fprintf(stderr, "%-6s %s %s %s\n", 300 header, progname, mainfuncs[i].name, mainfuncs[i].usage); 301 } 302 fprintf(stderr, "\n"); 303 fprintf(stderr, "All times in nanoseconds!\n"); 304 305 exit(1); 306} 307 308int 309main(int argc, char *argv[]) 310{ 311 progname = argv[0]; 312 if (argc < 2) 313 usage(1); 314 315 command = argv[1]; 316 317 argv += 2; 318 argc -= 2; 319 320 int i; 321 for (i = 0; i < numfuncs; ++i) { 322 if (strcmp(command, mainfuncs[i].name) != 0) 323 continue; 324 325 mainfuncs[i].func(argc, argv); 326 exit(0); 327 } 328 329 usage(1); 330} 331