Received: from [140.186.70.92] (port=39999 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PDcb9-0004HB-9J for qemu-devel@nongnu.org; Wed, 03 Nov 2010 08:37:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PDcZU-0006Ar-Gz for qemu-devel@nongnu.org; Wed, 03 Nov 2010 08:36:10 -0400 Received: from adelie.canonical.com ([91.189.90.139]:46582) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PDcZU-0006Ak-6Z for qemu-devel@nongnu.org; Wed, 03 Nov 2010 08:36:08 -0400 Received: from loganberry.canonical.com ([91.189.90.37]) by adelie.canonical.com with esmtp (Exim 4.69 #1 (Debian)) id 1PDcZR-0007oS-32 for ; Wed, 03 Nov 2010 12:36:05 +0000 Received: from loganberry.canonical.com (localhost [127.0.0.1]) by loganberry.canonical.com (Postfix) with ESMTP id 4AE8C2E8106 for ; Wed, 3 Nov 2010 12:35:51 +0000 (UTC) MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Date: Wed, 03 Nov 2010 12:28:32 -0000 From: eslay <626781@bugs.launchpad.net> Sender: bounces@canonical.com References: <20100830121607.11153.43460.malonedeb@soybean.canonical.com> Message-Id: <20101103122832.8354.79566.malone@potassium.ubuntu.com> Errors-To: bounces@canonical.com Subject: [Qemu-devel] [Bug 626781] Re: Live migration: bandwitdth calculation and rate limiting not working Reply-To: Bug 626781 <626781@bugs.launchpad.net> List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Hi, I issued a bug report (626781) months ago. Any news on it? By the way, I think it is really a bug other than a question. Regards, eslay ** Changed in: qemu Status: New =3D> Invalid ** Converted to question: https://answers.launchpad.net/qemu/+question/132364 -- = Live migration: bandwitdth calculation and rate limiting not working https://bugs.launchpad.net/bugs/626781 You received this bug notification because you are a member of qemu- devel-ml, which is subscribed to QEMU. Status in QEMU: Invalid Bug description: I am using QEMU 0.12.5 to perform live migration between 2 Linux hosts. One= Linux Host has 6 cores and 24G RAM, the other has 2 cores and 16G RAM. For= each host, I have one Ethernet interface for NFS storage, another interfac= e for live migration and a third interface for the VM to communicate to out= side network. Each interface has 1G bandwidth. It is observed that programs like below (which generates dirty pages very q= uickly) will hang the live migration: #include #include main() { = unsigned char *array; = long int i,j,k; unsigned char c; long int loop=3D0; array=3Dmalloc(1024*1024*1024); while(1) { for(i=3D0;i<1024;i++) { c=3D0; for(j=3D0;j<1024;j++) { c++; for(k=3D0;k<1024;k++) { array[i*1024*1024+j*1024+k]=3Dc; } } } loop++; if(loop%256=3D=3D0) printf("%ld\n",loop); } } It is observed that the traffic down time (measured by "ping -f" from a 3rd= host) has dependency on RAM size of the virtual machine: RAM Size Traffic Down Time Total Migration Time 1024M 0.5s 33s 2048M 0.7s 34s 4096M 2.7s 39s 8912M 5.3s 45s 16384M 7.2s 61s Using the command "migrate_set_downtime" in QEMU console won't improve the = problem. Function ram_save_live() in "vl.c" shows that live migration has three stag= es: Stage 1 is some preparation work. = Stage 2 is to transfer VM RAM to target host and keep the VM alive at sourc= e host. In Stage 2, the realtime migration bandwidth is calculated (Line 30= 99~3117 in vl.c). At the end of Stage 2 (Line 3130), the expected left time= of RAM transmission is calculated (Left RAM Size / Calculated Bandwidth). = If the expected left time is less than the max migration down time, Stage 2= is ended and Stage 3 starts. = Stage 3 is to stop the VM at the source host, transfer left RAM at full spe= ed, and then start the VM at the target host. The period of Stage 3 is beli= eved to be the period when the outside lost connection to the VM. = This is how live migration is supposed to work. There is a parameter max_throttle in "migration.c", which sets the max allo= wed bandwidth for rate limiting. The default value of this parameter is 32M= b/s (if not using command "migrate_set_speed" to change the value). But it = does not matter because the rate limiting faction does not work anyway. The= re is another parameter max_downtime in "migration.c", which sets the max a= llowed traffic down time for live migration.By default the value is set to = 30ms (if not using command "migrate_set_downtime" to change the value). Thi= s value to way too small, so if using the source code above, live migration= will hang. Stage 2 will never end since the expected left time would never= be less than 30ms. However, changing the parameter to something like 1000m= s will solve the hanging problem. After changing the default value of max_downtime, the long traffic down tim= e problem still exits. The following faults are found: = a) The bandwidth calculation in ram_save_live() (the first attachment) is w= rong. The bandwidth should equal to data transferred divided by the period = of transmission time. The period of transmission time should be the interva= l between two consecutive calls of function ram_save_live(), which is usual= ly 100ms (There should be a timer interrupt to control this). However, what= the code use is the execution time of the while loop between Line 3102 and= 3109. That is usually 2~5ms! This will yield to unreasonable large bandwid= th (6~12Gb/s), and in turn will make the estimated execution time of Stage = 3 inaccurate. For example, if the estimated execution time of Stage 3 is 90= 0ms, the actual execution time can be like10s! = b) The rate limiting function (qemu_file_rate_limit() which calls buffered_= rate_limit() in "buffered_file.c") does not work at all. No matter what par= ameters are set, the rate limiting function behaves the same: in Stage 2, i= n most time the migration bandwidth is ~400 Mb/s. When a certain condition = is fulfilled (I don't know exactly what condition but definitely not the nu= mber of iteration times), QEMU will read the VM RAM at full speed and throw= everything to the Ethernet link. This stalls the CPU and extend the execut= ion time of ram_save_live() to up to 6 seconds. Correspondingly, 6 seconds = of traffic downtime is seen during Stage 2. (What the algorithm assumes is = actually no traffic down time during Stage 2). = So the fundamental functions do not work when it comes to traffic down time= of live migration. However, problem a) and b) make the algorithm very easy= to enter Stage 3. The calculated bandwidth is ridiculously large, so as lo= ng as the max down time is not set to a value like 30ms and no extensive me= mory modification during migration, things will be finished. I have a dirty fix to the problem. I assume in Stage 2, ram_save_live() is = called every 100ms and each time ram_save_live() is called, no more than 10= 0Mb data can be transferred: static int count=3D0; static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque) { ram_addr_t addr; uint64_t bytes_transferred_last; double bwidth =3D 0; uint64_t expected_time =3D 0; // int64_t interval=3D0; bool flag=3Dtrue; uint64_t bytes_transferred2=3D0; if (stage < 0) { cpu_physical_memory_set_dirty_tracking(0); return 0; } // printf("ram_save_live: stage=3D %d\n",stage); if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) !=3D 0) { qemu_file_set_error(f); return 0; } if (stage =3D=3D 1) { bytes_transferred =3D 0; /* Make sure all dirty bits are set */ for (addr =3D 0; addr < last_ram_offset; addr +=3D TARGET_PAGE_SIZE= ) { if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) cpu_physical_memory_set_dirty(addr); } /* Enable dirty memory tracking */ cpu_physical_memory_set_dirty_tracking(1); qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE); } bytes_transferred_last =3D bytes_transferred; bwidth =3D get_clock(); while(bytes_transferred2<=3D1024*1024*1024/8/10) { // while ((!qemu_file_rate_limit(f))&&(bytes_transferred2<=3D1024*1024*1= 024/8/10)) { int ret; ret =3D ram_save_block(f); bytes_transferred +=3D ret * TARGET_PAGE_SIZE; bytes_transferred2 +=3D ret * TARGET_PAGE_SIZE; if (ret =3D=3D 0) /* no more blocks */ break; } count ++; bwidth =3D get_clock()-bwidth; if(bwidth<100000000) { bwidth=3D100000000; flag=3Dfalse; } if(flag) printf("ram_save_live: interval =3D %ld ms, count=3D %d\n",(in= t64_t)bwidth/1000000,count); bwidth =3D (bytes_transferred - bytes_transferred_last) / bwidth ; /* if we haven't transferred anything this round, force expected_time t= o a * a very high value, but without crashing */ if (bwidth =3D=3D 0) bwidth =3D 0.000001; if (bwidth > 1024*1024*1024/1000000000/8) bwidth =3D 1.024/8; /* try transferring iterative blocks of memory */ if (stage =3D=3D 3) { /* flush all remaining blocks regardless of rate limiting */ while (ram_save_block(f) !=3D 0) { bytes_transferred +=3D TARGET_PAGE_SIZE; } cpu_physical_memory_set_dirty_tracking(0); } qemu_put_be64(f, RAM_SAVE_FLAG_EOS); expected_time =3D ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; printf("ram_save_live: stage =3D %d, bwidth =3D %lf Mb/s, expected_time= =3D %ld ms, migrate_max_downtime =3D %ld ms\n",stage,bwidth*1000*8,expecte= d_time/1000000, migrate_max_downtime()/1000000); return (stage =3D=3D 2) && (expected_time <=3D migrate_max_downtime()); } For an empty VM, the dirty fix extends the total migration time to ~2 minut= es (with 15G RAM). But the traffic down time can be controlled to ~1 second= . The actual migration bandwidth is ~700Mb/s (all the time). This fix is very environment specific (won't work with, say, with 10G link)= . A thorough fix is needed for this problem.