int main(int argc, char** argv){ #ifdef MPI MPI_Init(&argc, &argv); #endif int power; init_msr(); struct rapl_data r[NUM_SOCKETS]; int socket; for(socket=0; socket<NUM_SOCKETS; socket++) r[socket].flags = RDF_REENTRANT | RDF_INIT; // Initialize r for(socket=0; socket<NUM_SOCKETS; socket++) read_rapl_data(socket, &r[socket]); sleep(1); perform_rapl_measurement(r); for(power=5; power<150; power+=10) { rapl_set_test(power); sleep(10); perform_rapl_measurement(r); } finalize_msr(); #ifdef MPI MPI_Finalize(); #endif return 0; }
void rapl_init(struct rapl_state_s *s, FILE *f, int print_header){ int socket; init_msr(); //parse_opts( argc, argv ); //fprintf(stderr, "%s::%d returned from parse_opts\n", __FILE__, __LINE__); s->f = f; if(print_header) print_rapl_state_header(s); for(socket=0; socket<NUM_PACKAGES; socket++){ get_rapl_power_unit( socket, &(s->power_unit[socket]) ); get_power_info( socket, PKG_DOMAIN, &(s->power_info[socket][PKG_DOMAIN]), &(s->power_unit[socket]) ); #ifdef ARCH_062D get_power_info( socket, DRAM_DOMAIN, &(s->power_info[socket][DRAM_DOMAIN]), &(s->power_unit[socket]) ); #endif get_power_limit( socket, PKG_DOMAIN, &(s->power_limit[socket][PKG_DOMAIN]), &(s->power_unit[socket]) ); get_power_limit( socket, PP0_DOMAIN, &(s->power_limit[socket][PP0_DOMAIN]), &(s->power_unit[socket]) ); #ifdef ARCH_062D get_power_limit( socket, DRAM_DOMAIN, &(s->power_limit[socket][DRAM_DOMAIN]), &(s->power_unit[socket]) ); #endif get_energy_status( socket, PKG_DOMAIN, NULL, &(s->power_unit[socket]), &s->last_raw_joules[socket][PKG_DOMAIN]); get_energy_status( socket, PP0_DOMAIN, NULL, &(s->power_unit[socket]) , &s->last_raw_joules[socket][PP0_DOMAIN]); #ifdef ARCH_062D get_energy_status( socket, DRAM_DOMAIN, NULL, &(s->power_unit[socket]) , &s->last_raw_joules[socket][DRAM_DOMAIN]); #endif } gettimeofday( &(s->prev), NULL ); }
void get_env_variables(struct rapl_state_s *s){ char *env = NULL; int package; int retVal = -1; env = getenv("READ_ONLY"); if(env == NULL){ //Read only flag has not been set. //Ensure that it is still zero. s->mode.read_only_flag = 0; } if(env){ s->mode.read_only_flag = strtoll(env,NULL,0); } env = getenv("READ_WRITE"); if(env == NULL){ //Read_write flag has not been set. //Ensure that it is still zero. s->mode.read_write_flag = 0; } if(env){ s->mode.read_write_flag = strtoll(env,NULL,0); } /* We are dealing with MSRs and need to be very careful, hence, if the * environment variables contain any thing other than 1, none of the code * should run and it should default to the dry-run. * The dry_run_flag is always 1. * If read_write_flag is 1, it should not matter what the value of read_only_flag is. * * If read_only_flag is 1, the read_write_flag has to be ZERO. * * */ if(s->mode.dry_run_flag == 1){ retVal = init_msr(); if(retVal == -1){ fprintf(stderr, "Error in initialization. Exiting.\n"); _exit(EXIT_FAILURE); } /*READ_ONLY_MODE*/ if(s->mode.read_only_flag == 1){ /*Need to determine what to do here. Output should probably be a file * with the measured power values. So, call init_msr(), * followed by the get_rapl_data(), followed by finalize_msr(). */ fprintf(stdout, "\nIn READ-ONLY mode.\n"); //Now that the permissions are correct, first disable turbo. for(package=0;package<NUM_PACKAGES; package++){ disable_turbo(package); } // You want to do this in here and not in rapl_init because it is safer to do it in here. // Also, in the dry run, none of this info should be printed. // You need to have read_msr access to be able to get the info/limit/status. print_rapl_state_header(s); for(package=0; package<NUM_PACKAGES; package++){ get_all_info( package, s); get_all_limit( package, s); get_all_status(package, s); gettimeofday( &(s->start[package]), NULL ); } } /*READ_WRITE_MODE. Care should be taken that the user has the right permissions, and * that even if the environment variable is set, you can't write to MSRs unless * you have the right permissions. * Also, if both the read_write and read_only_flag are set, it should default to read_only * under the assumption that the use is not clear about how to use the library correctly */ if(s->mode.read_write_flag == 1 && s->mode.read_only_flag == 0){ fprintf(stdout, "\nIn READ-WRITE mode.\n"); //Now that the permissions are correct, first disable turbo. for(package=0;package<NUM_PACKAGES; package++){ disable_turbo(package); } // You want to do this in here and not in rapl_init because it is safer to do it in here. print_rapl_state_header(s); for(package=0; package<NUM_PACKAGES; package++){ get_all_info( package, s); get_all_limit( package, s); get_all_status(package, s); gettimeofday( &(s->start[package]), NULL ); } //Write to the POWER registers set_power_bounds(); } } }
int main(int argc, char ** argv) { short israpl = 0; char next = '\0'; ISVERBOSE = 0; init_msr(); uint64_t msr_data; uint64_t msr; unsigned thread; int i; for (i = 0; i < argc; i++) { if (argv[i][0] == '-') { switch(argv[i][1]) { case 'v': // do verbose stuff ISVERBOSE = 1; break; case 'r': // read an msr if (argc - i < 3) { fprintf(stderr, "Error: not enough parameters to read flag\n"); generic_error(i, argv); } thread = atoi(argv[++i]); msr = strtol(argv[++i], NULL, 16); read_msr_by_idx(thread, msr, &msr_data); fprintf(stdout, "%lx\n", msr_data); break; case 'w': // write an msr if (argc - i < 4) { fprintf(stderr, "Error: not enough parameters to write flag\n"); generic_error(i, argv); } thread = atoi(argv[++i]); msr = strtol(argv[++i], NULL, 16); msr_data = strtol(argv[++i], NULL, 16); write_msr_by_idx(thread, msr, msr_data); break; case 'i': // do interactive stuff fprintf(stdout, "Interactive mode is not yet available\n"); break; case '-': // go deeper down the rabbit hole switch(argv[i][2]) { case 'h': // print help stuff help_stuff(); break; } return 0; case 'p': // print out stuff printing_functions(&i, argc, argv); break; case 's': // set a socket limit or set to default set_functions(&i, argc, argv); break; case 'l': // list available events list_functions(&i, argc, argv); return 0; } } } // if (israpl) // { // do_rapl_stuff(isverbose); // } return 0; }
int main(int argc, char *argv[]) { int c, error, gdb_port, err, bvmcons; int dump_guest_memory, max_vcpus, mptgen; int rtc_localtime; struct vmctx *ctx; uint64_t rip; size_t memsize; bvmcons = 0; dump_guest_memory = 0; progname = basename(argv[0]); gdb_port = 0; guest_ncpus = 1; memsize = 256 * MB; mptgen = 1; rtc_localtime = 1; while ((c = getopt(argc, argv, "abehuwxACHIPWYp:g:c:s:m:l:U:")) != -1) { switch (c) { case 'a': x2apic_mode = 0; break; case 'A': acpi = 1; break; case 'b': bvmcons = 1; break; case 'p': if (pincpu_parse(optarg) != 0) { errx(EX_USAGE, "invalid vcpu pinning " "configuration '%s'", optarg); } break; case 'c': guest_ncpus = atoi(optarg); break; case 'C': dump_guest_memory = 1; break; case 'g': gdb_port = atoi(optarg); break; case 'l': if (lpc_device_parse(optarg) != 0) { errx(EX_USAGE, "invalid lpc device " "configuration '%s'", optarg); } break; case 's': if (pci_parse_slot(optarg) != 0) exit(1); else break; case 'm': error = vm_parse_memsize(optarg, &memsize); if (error) errx(EX_USAGE, "invalid memsize '%s'", optarg); break; case 'H': guest_vmexit_on_hlt = 1; break; case 'I': /* * The "-I" option was used to add an ioapic to the * virtual machine. * * An ioapic is now provided unconditionally for each * virtual machine and this option is now deprecated. */ break; case 'P': guest_vmexit_on_pause = 1; break; case 'e': strictio = 1; break; case 'u': rtc_localtime = 0; break; case 'U': guest_uuid_str = optarg; break; case 'w': strictmsr = 0; break; case 'W': virtio_msix = 0; break; case 'x': x2apic_mode = 1; break; case 'Y': mptgen = 0; break; case 'h': usage(0); default: usage(1); } } argc -= optind; argv += optind; if (argc != 1) usage(1); vmname = argv[0]; ctx = vm_open(vmname); if (ctx == NULL) { perror("vm_open"); exit(1); } if (guest_ncpus < 1) { fprintf(stderr, "Invalid guest vCPUs (%d)\n", guest_ncpus); exit(1); } max_vcpus = num_vcpus_allowed(ctx); if (guest_ncpus > max_vcpus) { fprintf(stderr, "%d vCPUs requested but only %d available\n", guest_ncpus, max_vcpus); exit(1); } fbsdrun_set_capabilities(ctx, BSP); if (dump_guest_memory) vm_set_memflags(ctx, VM_MEM_F_INCORE); err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL); if (err) { fprintf(stderr, "Unable to setup memory (%d)\n", err); exit(1); } error = init_msr(); if (error) { fprintf(stderr, "init_msr error %d", error); exit(1); } init_mem(); init_inout(); pci_irq_init(ctx); ioapic_init(ctx); rtc_init(ctx, rtc_localtime); sci_init(ctx); /* * Exit if a device emulation finds an error in it's initilization */ if (init_pci(ctx) != 0) exit(1); if (gdb_port != 0) init_dbgport(gdb_port); if (bvmcons) init_bvmcons(); error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip); assert(error == 0); /* * build the guest tables, MP etc. */ if (mptgen) { error = mptable_build(ctx, guest_ncpus); if (error) exit(1); } error = smbios_build(ctx); assert(error == 0); if (acpi) { error = acpi_build(ctx, guest_ncpus); assert(error == 0); } /* * Change the proc title to include the VM name. */ setproctitle("%s", vmname); /* * Add CPU 0 */ fbsdrun_addcpu(ctx, BSP, BSP, rip); /* * Head off to the main event dispatch loop */ mevent_dispatch(); exit(1); }
int main(int argc, char *argv[]) { int c, error, dbg_port, gdb_port, err, bvmcons; int max_vcpus, mptgen, memflags; int rtc_localtime; bool gdb_stop; struct vmctx *ctx; uint64_t rip; size_t memsize; char *optstr; bvmcons = 0; progname = basename(argv[0]); dbg_port = 0; gdb_port = 0; gdb_stop = false; guest_ncpus = 1; sockets = cores = threads = 1; maxcpus = 0; memsize = 256 * MB; mptgen = 1; rtc_localtime = 1; memflags = 0; optstr = "abehuwxACHIPSWYp:g:G:c:s:m:l:U:"; while ((c = getopt(argc, argv, optstr)) != -1) { switch (c) { case 'a': x2apic_mode = 0; break; case 'A': acpi = 1; break; case 'b': bvmcons = 1; break; case 'p': if (pincpu_parse(optarg) != 0) { errx(EX_USAGE, "invalid vcpu pinning " "configuration '%s'", optarg); } break; case 'c': if (topology_parse(optarg) != 0) { errx(EX_USAGE, "invalid cpu topology " "'%s'", optarg); } break; case 'C': memflags |= VM_MEM_F_INCORE; break; case 'g': dbg_port = atoi(optarg); break; case 'G': if (optarg[0] == 'w') { gdb_stop = true; optarg++; } gdb_port = atoi(optarg); break; case 'l': if (lpc_device_parse(optarg) != 0) { errx(EX_USAGE, "invalid lpc device " "configuration '%s'", optarg); } break; case 's': if (pci_parse_slot(optarg) != 0) exit(1); else break; case 'S': memflags |= VM_MEM_F_WIRED; break; case 'm': error = vm_parse_memsize(optarg, &memsize); if (error) errx(EX_USAGE, "invalid memsize '%s'", optarg); break; case 'H': guest_vmexit_on_hlt = 1; break; case 'I': /* * The "-I" option was used to add an ioapic to the * virtual machine. * * An ioapic is now provided unconditionally for each * virtual machine and this option is now deprecated. */ break; case 'P': guest_vmexit_on_pause = 1; break; case 'e': strictio = 1; break; case 'u': rtc_localtime = 0; break; case 'U': guest_uuid_str = optarg; break; case 'w': strictmsr = 0; break; case 'W': virtio_msix = 0; break; case 'x': x2apic_mode = 1; break; case 'Y': mptgen = 0; break; case 'h': usage(0); default: usage(1); } } argc -= optind; argv += optind; if (argc != 1) usage(1); vmname = argv[0]; ctx = do_open(vmname); max_vcpus = num_vcpus_allowed(ctx); if (guest_ncpus > max_vcpus) { fprintf(stderr, "%d vCPUs requested but only %d available\n", guest_ncpus, max_vcpus); exit(1); } fbsdrun_set_capabilities(ctx, BSP); vm_set_memflags(ctx, memflags); err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL); if (err) { fprintf(stderr, "Unable to setup memory (%d)\n", errno); exit(1); } error = init_msr(); if (error) { fprintf(stderr, "init_msr error %d", error); exit(1); } init_mem(); init_inout(); atkbdc_init(ctx); pci_irq_init(ctx); ioapic_init(ctx); rtc_init(ctx, rtc_localtime); sci_init(ctx); /* * Exit if a device emulation finds an error in its initilization */ if (init_pci(ctx) != 0) exit(1); if (dbg_port != 0) init_dbgport(dbg_port); if (gdb_port != 0) init_gdb(ctx, gdb_port, gdb_stop); if (bvmcons) init_bvmcons(); if (lpc_bootrom()) { if (vm_set_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, 1)) { fprintf(stderr, "ROM boot failed: unrestricted guest " "capability not available\n"); exit(1); } error = vcpu_reset(ctx, BSP); assert(error == 0); } error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip); assert(error == 0); /* * build the guest tables, MP etc. */ if (mptgen) { error = mptable_build(ctx, guest_ncpus); if (error) exit(1); } error = smbios_build(ctx); assert(error == 0); if (acpi) { error = acpi_build(ctx, guest_ncpus); assert(error == 0); } if (lpc_bootrom()) fwctl_init(); #ifndef WITHOUT_CAPSICUM caph_cache_catpages(); if (caph_limit_stdout() == -1 || caph_limit_stderr() == -1) errx(EX_OSERR, "Unable to apply rights for sandbox"); if (cap_enter() == -1 && errno != ENOSYS) errx(EX_OSERR, "cap_enter() failed"); #endif /* * Change the proc title to include the VM name. */ setproctitle("%s", vmname); /* * Add CPU 0 */ fbsdrun_addcpu(ctx, BSP, BSP, rip); /* * Head off to the main event dispatch loop */ mevent_dispatch(); exit(1); }