From 1eed0db2ecc3677fd6d2a313f6f04f989889e53b Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sat, 27 Aug 2011 04:13:50 +0000 Subject: [PATCH] - VirtualBox wrapper: - change names of CPU and network-limiting functions to express their units (always do this). - wrapper monitors status.suspend_network and suspends/resumes network accordingly - wrapper sets CPU and network bandwidth limits on startup (Rom: should this be done before run() rather than after?) Note: App versions using this wrapper should always have in their version.xml, to tell the client that the app throttles itself. svn path=/trunk/boinc/; revision=24054 --- checkin_notes | 16 +++++++++++++++ samples/vboxwrapper/vbox.cpp | 22 ++++++++++++-------- samples/vboxwrapper/vbox.h | 7 +++++-- samples/vboxwrapper/vboxwrapper.cpp | 31 +++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/checkin_notes b/checkin_notes index c7f0f84f0c..4276ce1943 100644 --- a/checkin_notes +++ b/checkin_notes @@ -5130,3 +5130,19 @@ David 26 Aug 2011 sched_version.cpp sched_send.cpp Makefile.am + +David 26 Aug 2011 + - VirtualBox wrapper: + - change names of CPU and network-limiting functions + to express their units (always do this). + - wrapper monitors status.suspend_network + and suspends/resumes network accordingly + - wrapper sets CPU and network bandwidth limits on startup + (Rom: should this be done before run() rather than after?) + Note: App versions using this wrapper should always have + in their version.xml, + to tell the client that the app throttles itself. + + samples/vboxwrapper/ + vbox.cpp,h + vboxwrapper.cpp diff --git a/samples/vboxwrapper/vbox.cpp b/samples/vboxwrapper/vbox.cpp index 36804d241c..1d6f6e5fa4 100644 --- a/samples/vboxwrapper/vbox.cpp +++ b/samples/vboxwrapper/vbox.cpp @@ -54,6 +54,7 @@ VBOX_VM::VBOX_VM() { memory_size_mb.clear(); image_filename.clear(); suspended = false; + network_suspended = false; enable_network = false; enable_shared_directory = false; } @@ -495,11 +496,11 @@ int VBOX_VM::register_vm() { return retval; } - // Enable the network adapter if a network connection is required. // - set_network_access(enable_network); - + if (enable_network) { + set_network_access(true); + } // Enable the shared folder if a shared folder is specified. // @@ -783,6 +784,8 @@ int VBOX_VM::set_network_access(bool enabled) { char buf[256]; int retval; + network_suspended = !enabled; + if (enabled) { command = "modifyvm \"" + vm_name + "\" "; command += "--cableconnected1 on "; @@ -820,14 +823,15 @@ int VBOX_VM::set_network_access(bool enabled) { } -int VBOX_VM::set_cpu_throttle(int throttle_speed) { +int VBOX_VM::set_cpu_usage_fraction(double x) { string command; string output; char buf[256]; int retval; - sprintf(buf, "%d", throttle_speed); - + // the arg to modifyvm is percentage + // + sprintf(buf, "%d", (int)(x*100.)); command = "modifyvm \"" + vm_name + "\" "; command += "--cpuexecutioncap "; command += buf; @@ -849,14 +853,16 @@ int VBOX_VM::set_cpu_throttle(int throttle_speed) { } -int VBOX_VM::set_network_throttle(int throttle_speed) { +int VBOX_VM::set_network_max_bytes_sec(double x) { string command; string output; char buf[256]; int retval; - sprintf(buf, "%d", throttle_speed); + // the argument to modifyvm is in Kbps + // + sprintf(buf, "%d", (int)(x*8./1000.)); command = "modifyvm \"" + vm_name + "\" "; command += "--nicspeed1 "; command += buf; diff --git a/samples/vboxwrapper/vbox.h b/samples/vboxwrapper/vbox.h index e22c7da246..a6aa8da5d7 100644 --- a/samples/vboxwrapper/vbox.h +++ b/samples/vboxwrapper/vbox.h @@ -36,7 +36,10 @@ struct VBOX_VM { std::string vm_name; // unique name for the VM bool suspended; + bool network_suspended; + // whether network access is temporarily suspended bool enable_network; + // whether to allow network access at all bool enable_shared_directory; void poll(); @@ -54,8 +57,8 @@ struct VBOX_VM { int deregister_vm(); int startvm(); int set_network_access(bool enabled); - int set_cpu_throttle(int throttle_speed); - int set_network_throttle(int throttle_speed); + int set_cpu_usage_fraction(double); + int set_network_max_bytes_sec(double); static int initialize(); static int generate_vm_root_dir( std::string& dir ); diff --git a/samples/vboxwrapper/vboxwrapper.cpp b/samples/vboxwrapper/vboxwrapper.cpp index 13b379e35a..d7e3016250 100644 --- a/samples/vboxwrapper/vboxwrapper.cpp +++ b/samples/vboxwrapper/vboxwrapper.cpp @@ -184,6 +184,26 @@ int main(int argc, char** argv) { boinc_finish(retval); } + // set CPU and network throttling if needed + // + double x = aid.global_prefs.cpu_usage_limit; + if (x && x<100) { + vm.set_cpu_usage_fraction(x/100.); + } + + // vbox doesn't distinguish up and down bandwidth; use the min of the prefs + // + x = aid.global_prefs.max_bytes_sec_up; + double y = aid.global_prefs.max_bytes_sec_down; + if (y) { + if (!x || y