Skip to contentSkip to conversation

Basic TSC-Scaling support v2

PATCH Applied Joerg Roedel 6 participants 16 messages Jul 7, 2011 – Jul 19, 2011 KVM

lore
Share & tools
lore
#0 lore raw

cover Basic TSC-Scaling support v2

Hi Avi, Marcelo,

here is v2 of the patches to support setting the guests tsc-frequency from the qemu command line. This version addresses the comment from Avi on the first version. To reflect that units can be given to the frequency, the parameter was renamed from tsc_khz to tsc_freq.

Thanks,

Joerg

Diffstat:

 cutils.c            |   16 +++++++++++-----
 qemu-common.h       |    2 ++
 target-i386/cpu.h   |    1 +
 target-i386/cpuid.c |   13 +++++++++++++
 target-i386/kvm.c   |   18 +++++++++++++++++-
 5 files changed, 44 insertions(+), 6 deletions(-)
#1 lore raw

PATCH 1/3 qemu: Add strtosz_suffix_unit function

This function does the same as the strtosz_suffix function except that it allows to specify the unit to which the k/M/B/T suffixes apply. This function will be used later to parse the tsc-frequency from the command-line.

---
 cutils.c      |   16 +++++++++++-----
 qemu-common.h |    2 ++
 2 files changed, 13 insertions(+), 5 deletions(-)
cutils.c +11 -5
index f9a7e36..28049e0 100644
@@ -322,7 +322,8 @@ int fcntl_setfl(int fd, int flag)
  * value must be terminated by whitespace, ',' or '\0'. Return -1 on
  * error.
  */
-int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
+int64_t strtosz_suffix_unit(const char *nptr, char **end,
+                            const char default_suffix, int64_t unit)
 {
     int64_t retval = -1;
     char *endptr;
@@ -362,20 +363,20 @@ int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
         }
         break;
     case STRTOSZ_DEFSUFFIX_KB:
-        mul = 1 << 10;
+        mul = unit;
         break;
     case 0:
         if (mul_required) {
             goto fail;
         }
     case STRTOSZ_DEFSUFFIX_MB:
-        mul = 1ULL << 20;
+        mul = unit * unit;
         break;
     case STRTOSZ_DEFSUFFIX_GB:
-        mul = 1ULL << 30;
+        mul = unit * unit * unit;
         break;
     case STRTOSZ_DEFSUFFIX_TB:
-        mul = 1ULL << 40;
+        mul = unit * unit * unit * unit;
         break;
     default:
         goto fail;
@@ -405,6 +406,11 @@ fail:
     return retval;
 }
 
+int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
+{
+        return strtosz_suffix_unit(nptr, end, default_suffix, 1024);
+}
+
 int64_t strtosz(const char *nptr, char **end)
 {
     return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
qemu-common.h +2 -0
index 109498d..854031a 100644
@@ -186,6 +186,8 @@ int fcntl_setfl(int fd, int flag);
 #define STRTOSZ_DEFSUFFIX_B	'B'
 int64_t strtosz(const char *nptr, char **end);
 int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
+int64_t strtosz_suffix_unit(const char *nptr, char **end,
+                            const char default_suffix, int64_t unit);
 
 /* path.c */
 void init_paths(const char *prefix);
Signature · 1 line
1.7.4.1
#2 lore raw

Re: [PATCH 1/3] qemu: Add strtosz_suffix_unit function

Quoted 51 lines · Joerg Roedel

This function does the same as the strtosz_suffix function except that it allows to specify the unit to which the k/M/B/T suffixes apply. This function will be used later to parse the tsc-frequency from the command-line.

---
 cutils.c      |   16 +++++++++++-----
 qemu-common.h |    2 ++
 2 files changed, 13 insertions(+), 5 deletions(-)
cutils.c +6 -5
index f9a7e36..28049e0 100644
@@ -322,7 +322,8 @@ int fcntl_setfl(int fd, int flag)
  * value must be terminated by whitespace, ',' or '\0'. Return -1 on
  * error.
  */
-int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
+int64_t strtosz_suffix_unit(const char *nptr, char **end,
+                            const char default_suffix, int64_t unit)
 {
     int64_t retval = -1;
     char *endptr;
@@ -362,20 +363,20 @@ int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
         }
         break;
     case STRTOSZ_DEFSUFFIX_KB:
-        mul = 1 << 10;
+        mul = unit;
         break;
     case 0:
         if (mul_required) {
             goto fail;
         }
     case STRTOSZ_DEFSUFFIX_MB:
-        mul = 1ULL << 20;
+        mul = unit * unit;
         break;
     case STRTOSZ_DEFSUFFIX_GB:
-        mul = 1ULL << 30;
+        mul = unit * unit * unit;
         break;
     case STRTOSZ_DEFSUFFIX_TB:
-        mul = 1ULL << 40;
+        mul = unit * unit * unit * unit;
         break;
     default:
         goto fail;

cutils.c fail: · line 407

Quoted 2 lines
cutils.c +0 -0
@@ -405,6 +406,11 @@ fail:
     return retval;
 }

Why would anyone ever call this function with an unit argument other than 1000 or 1024?

Without such a use case, I'd rather give strtosz_suffix() a flag parameter to pick SI prefixes (multiples of 1000) vs. binary prefixes (multiples of 1024).

[...]

#3 lore raw

PATCH 2/3 qemu-x86: Add tsc_freq option to -cpu

In reply to Joerg Roedel

To let the user configure the desired tsc frequency for the guest if running in KVM.

---
 target-i386/cpu.h   |    1 +
 target-i386/cpuid.c |   13 +++++++++++++
 2 files changed, 14 insertions(+), 0 deletions(-)
target-i386/cpu.h +1 -0
index cdf68ff..399e124 100644
@@ -743,6 +743,7 @@ typedef struct CPUX86State {
     uint32_t cpuid_kvm_features;
     uint32_t cpuid_svm_features;
     bool tsc_valid;
+    int tsc_khz;
     
     /* in order to simplify APIC support, we leave this pointer to the
        user */
target-i386/cpuid.c +13 -0
index e1ae3af..89e9623 100644
@@ -224,6 +224,7 @@ typedef struct x86_def_t {
     int family;
     int model;
     int stepping;
+    int tsc_khz;
     uint32_t features, ext_features, ext2_features, ext3_features;
     uint32_t kvm_features, svm_features;
     uint32_t xlevel;
@@ -704,6 +705,17 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
             } else if (!strcmp(featurestr, "model_id")) {
                 pstrcpy(x86_cpu_def->model_id, sizeof(x86_cpu_def->model_id),
                         val);
+            } else if (!strcmp(featurestr, "tsc_freq")) {
+                int64_t tsc_freq;
+                char *err;
+
+                tsc_freq = strtosz_suffix_unit(val, &err,
+                                               STRTOSZ_DEFSUFFIX_B, 1000);
+                if (!*val || *err) {
+                    fprintf(stderr, "bad numerical value %s\n", val);
+                    goto error;
+                }
+                x86_cpu_def->tsc_khz = tsc_freq / 1000;
             } else {
                 fprintf(stderr, "unrecognized feature %s\n", featurestr);
                 goto error;
@@ -872,6 +884,7 @@ int cpu_x86_register (CPUX86State *env, const char *cpu_model)
     env->cpuid_svm_features = def->svm_features;
     env->cpuid_ext4_features = def->ext4_features;
     env->cpuid_xlevel2 = def->xlevel2;
+    env->tsc_khz = def->tsc_khz;
     if (!kvm_enabled()) {
         env->cpuid_features &= TCG_FEATURES;
         env->cpuid_ext_features &= TCG_EXT_FEATURES;
Signature · 1 line
1.7.4.1
#4 lore raw

Re: [PATCH 2/3] qemu-x86: Add tsc_freq option to -cpu

Quoted 14 lines · Joerg Roedel

To let the user configure the desired tsc frequency for the guest if running in KVM.

---
 target-i386/cpu.h   |    1 +
 target-i386/cpuid.c |   13 +++++++++++++
 2 files changed, 14 insertions(+), 0 deletions(-)
target-i386/cpu.h +0 -0
index cdf68ff..399e124 100644

target-i386/cpu.h typedef struct CPUX86State { · line 746

Quoted 4 lines
target-i386/cpu.h +1 -0
@@ -743,6 +743,7 @@ typedef struct CPUX86State {
     uint32_t cpuid_kvm_features;
     uint32_t cpuid_svm_features;
     bool tsc_valid;
+    int tsc_khz;

This should be saved/restore in migration data (missing VMSTATE entry).

Quoted 48 lines
target-i386/cpu.h +0 -0
     /* in order to simplify APIC support, we leave this pointer to the
        user */
target-i386/cpuid.c +13 -0
index e1ae3af..89e9623 100644
@@ -224,6 +224,7 @@ typedef struct x86_def_t {
     int family;
     int model;
     int stepping;
+    int tsc_khz;
     uint32_t features, ext_features, ext2_features, ext3_features;
     uint32_t kvm_features, svm_features;
     uint32_t xlevel;
@@ -704,6 +705,17 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
             } else if (!strcmp(featurestr, "model_id")) {
                 pstrcpy(x86_cpu_def->model_id, sizeof(x86_cpu_def->model_id),
                         val);
+            } else if (!strcmp(featurestr, "tsc_freq")) {
+                int64_t tsc_freq;
+                char *err;
+
+                tsc_freq = strtosz_suffix_unit(val, &err,
+                                               STRTOSZ_DEFSUFFIX_B, 1000);
+                if (!*val || *err) {
+                    fprintf(stderr, "bad numerical value %s\n", val);
+                    goto error;
+                }
+                x86_cpu_def->tsc_khz = tsc_freq / 1000;
             } else {
                 fprintf(stderr, "unrecognized feature %s\n", featurestr);
                 goto error;
@@ -872,6 +884,7 @@ int cpu_x86_register (CPUX86State *env, const char *cpu_model)
     env->cpuid_svm_features = def->svm_features;
     env->cpuid_ext4_features = def->ext4_features;
     env->cpuid_xlevel2 = def->xlevel2;
+    env->tsc_khz = def->tsc_khz;
     if (!kvm_enabled()) {
         env->cpuid_features &= TCG_FEATURES;
         env->cpuid_ext_features &= TCG_EXT_FEATURES;
Signature · 7 lines
1.7.4.1


--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#5 lore raw

Re: [PATCH 2/3] qemu-x86: Add tsc_freq option to -cpu

Quoted 20 lines · Marcelo Tosatti
Quoted 13 lines · Joerg Roedel

To let the user configure the desired tsc frequency for the guest if running in KVM.

---

  target-i386/cpu.h   |    1 +
  target-i386/cpuid.c |   13 +++++++++++++
  2 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/target-i386/cpu.h b/target-i386/cpu.h index cdf68ff..399e124 100644
--- a/target-i386/cpu.h
+++ b/target-i386/cpu.h

Quoted 5 lines
 @@ -743,6 +743,7 @@ typedef struct CPUX86State {
      uint32_t cpuid_kvm_features;
      uint32_t cpuid_svm_features;
      bool tsc_valid;

+ int tsc_khz;

Quoted 1 line

This should be saved/restore in migration data (missing VMSTATE entry).

Why? It's static data. Traditionally we only migrate runtime data.

(although we've been talking about starting a naked qemu and pushing all of the configuration from the source).

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw

Re: [PATCH 2/3] qemu-x86: Add tsc_freq option to -cpu

Quoted 21 lines · Avi Kivity
Quoted 20 lines · Marcelo Tosatti
Quoted 18 lines · Joerg Roedel

To let the user configure the desired tsc frequency for the guest if running in KVM.

---

  target-i386/cpu.h   |    1 +
  target-i386/cpuid.c |   13 +++++++++++++
  2 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/target-i386/cpu.h b/target-i386/cpu.h index cdf68ff..399e124 100644
--- a/target-i386/cpu.h
+++ b/target-i386/cpu.h

 @@ -743,6 +743,7 @@ typedef struct CPUX86State {
      uint32_t cpuid_kvm_features;
      uint32_t cpuid_svm_features;
      bool tsc_valid;

+ int tsc_khz;

Quoted 6 lines
Quoted 1 line

This should be saved/restore in migration data (missing VMSTATE entry).

Why? It's static data. Traditionally we only migrate runtime data.

(although we've been talking about starting a naked qemu and pushing all of the configuration from the source).

Right.

#7 lore raw

Re: [PATCH 2/3] qemu-x86: Add tsc_freq option to -cpu

In reply to Avi Kivity
Quoted 21 lines · Avi Kivity
Quoted 20 lines · Marcelo Tosatti
Quoted 18 lines · Joerg Roedel

To let the user configure the desired tsc frequency for the guest if running in KVM.

---

  target-i386/cpu.h   |    1 +
  target-i386/cpuid.c |   13 +++++++++++++
  2 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/target-i386/cpu.h b/target-i386/cpu.h index cdf68ff..399e124 100644
--- a/target-i386/cpu.h
+++ b/target-i386/cpu.h

 @@ -743,6 +743,7 @@ typedef struct CPUX86State {
      uint32_t cpuid_kvm_features;
      uint32_t cpuid_svm_features;
      bool tsc_valid;

+ int tsc_khz;

Quoted 6 lines
Quoted 1 line

This should be saved/restore in migration data (missing VMSTATE entry).

Why? It's static data. Traditionally we only migrate runtime data.

(although we've been talking about starting a naked qemu and pushing all of the configuration from the source).

Hmm, I planned to do the VMSTATE thing in a follow-on patch-set. The plan is to read the VCPU tsc_freq at guest start time on !tsc-scale hosts and migrate it over so that the destination host can set the tsc-freq if it supports tsc-scaling.

Joerg

#8 lore raw

Re: [PATCH 2/3] qemu-x86: Add tsc_freq option to -cpu

Quoted 8 lines · Joerg Roedel
Quoted 3 lines

(although we've been talking about starting a naked qemu and pushing all of the configuration from the source).

Hmm, I planned to do the VMSTATE thing in a follow-on patch-set. The plan is to read the VCPU tsc_freq at guest start time on !tsc-scale hosts and migrate it over so that the destination host can set the tsc-freq if it supports tsc-scaling.

This can be done by a management tool if desired.

Signature · 1 line
error compiling committee.c: too many arguments to function
#9 lore raw

Re: [PATCH 2/3] qemu-x86: Add tsc_freq option to -cpu

Quoted 4 lines · Avi Kivity
Quoted 3 lines · Joerg Roedel
Quoted 2 lines

(although we've been talking about starting a naked qemu and

pushing all

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

of the configuration from the source).

Hmm, I planned to do the VMSTATE thing in a follow-on patch-set. The plan is to read the VCPU tsc_freq at guest start time on !tsc-scale hosts and migrate it over so that the destination host can set the tsc-freq if it supports tsc-scaling.

This can be done by a management tool if desired.

Although, if we do this unconditionally (that is, also for tsc-scale hosts) then we get stable tsc even without supplying a tsc frequency argument... need to think about this.

Signature · 1 line
error compiling committee.c: too many arguments to function
#10 lore raw

Re: [PATCH 2/3] qemu-x86: Add tsc_freq option to -cpu

Quoted 8 lines · Avi Kivity
Quoted 7 lines · Avi Kivity
Quoted 4 lines · Joerg Roedel

Hmm, I planned to do the VMSTATE thing in a follow-on patch-set. The plan is to read the VCPU tsc_freq at guest start time on !tsc-scale hosts and migrate it over so that the destination host can set the tsc-freq if it supports tsc-scaling.

Quoted 6 lines
Quoted 2 lines

This can be done by a management tool if desired.

Although, if we do this unconditionally (that is, also for tsc-scale hosts) then we get stable tsc even without supplying a tsc frequency argument... need to think about this.

It has the advantage that it "just works", without the need to extend management tools and the like. And it makes migration more transparent to the guests.

Joerg

#11 lore raw

Re: [PATCH 2/3] qemu-x86: Add tsc_freq option to -cpu

Quoted 12 lines · Joerg Roedel
Quoted 11 lines · Avi Kivity
Quoted 9 lines · Avi Kivity
Quoted 4 lines · Joerg Roedel

Hmm, I planned to do the VMSTATE thing in a follow-on patch-set. The plan is to read the VCPU tsc_freq at guest start time on !tsc-scale hosts and migrate it over so that the destination host can set the tsc-freq if it supports tsc-scaling.

This can be done by a management tool if desired.

Quoted 8 lines
Quoted 3 lines

Although, if we do this unconditionally (that is, also for tsc-scale hosts) then we get stable tsc even without supplying a tsc frequency argument... need to think about this.

It has the advantage that it "just works", without the need to extend management tools and the like. And it makes migration more transparent to the guests.

Yes, exactly. The flip side is that automagic stuff is sometimes unexpected and leads to breakage. I'm not sure what the right thing is.

Signature · 1 line
error compiling committee.c: too many arguments to function
#12 lore raw

PATCH 3/3 qemu-x86: Set tsc_khz in kvm when supported

In reply to Joerg Roedel

Make use of the KVM_TSC_CONTROL feature if available.

---
 target-i386/kvm.c |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)
target-i386/kvm.c +17 -1
index 10fb2c4..923d2d5 100644
@@ -354,6 +354,7 @@ int kvm_arch_init_vcpu(CPUState *env)
     uint32_t unused;
     struct kvm_cpuid_entry2 *c;
     uint32_t signature[3];
+    int r;
 
     env->cpuid_features &= kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX);
 
@@ -499,7 +500,22 @@ int kvm_arch_init_vcpu(CPUState *env)
 
     qemu_add_vm_change_state_handler(cpu_update_state, env);
 
-    return kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
+    r = kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
+    if (r)
+	    return r;
+
+#ifdef KVM_CAP_TSC_CONTROL
+    r = kvm_check_extension(env->kvm_state, KVM_CAP_TSC_CONTROL);
+    if (r && env->tsc_khz) {
+        r = kvm_vcpu_ioctl(env, KVM_SET_TSC_KHZ, env->tsc_khz);
+        if (r < 0) {
+            fprintf(stderr, "KVM_SET_TSC_KHZ failed\n");
+            return r;
+        }
+    }
+#endif
+
+    return 0;
 }
 
 void kvm_arch_reset_vcpu(CPUState *env)
Signature · 1 line
1.7.4.1
#13 lore raw

Re: [PATCH 3/3] qemu-x86: Set tsc_khz in kvm when supported

Quoted 28 lines · Joerg Roedel

Make use of the KVM_TSC_CONTROL feature if available.

---
 target-i386/kvm.c |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)
target-i386/kvm.c +5 -1
index 10fb2c4..923d2d5 100644
@@ -354,6 +354,7 @@ int kvm_arch_init_vcpu(CPUState *env)
     uint32_t unused;
     struct kvm_cpuid_entry2 *c;
     uint32_t signature[3];
+    int r;
 
     env->cpuid_features &= kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX);
 
@@ -499,7 +500,22 @@ int kvm_arch_init_vcpu(CPUState *env)
 
     qemu_add_vm_change_state_handler(cpu_update_state, env);
 
-    return kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
+    r = kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
+    if (r)
+	    return r;
+

target-i386/kvm.c int kvm_arch_init_vcpu(CPUState *env) · line 516

Quoted 10 lines
target-i386/kvm.c +10 -0
+#ifdef KVM_CAP_TSC_CONTROL
+    r = kvm_check_extension(env->kvm_state, KVM_CAP_TSC_CONTROL);
+    if (r && env->tsc_khz) {
+        r = kvm_vcpu_ioctl(env, KVM_SET_TSC_KHZ, env->tsc_khz);
+        if (r < 0) {
+            fprintf(stderr, "KVM_SET_TSC_KHZ failed\n");
+            return r;
+        }
+    }
+#endif

And this should be moved to kvm_arch_put_registers, in case level == KVM_PUT_FULL_STATE.

#14 lore raw

Re: [PATCH 3/3] qemu-x86: Set tsc_khz in kvm when supported

Quoted 24 lines · Marcelo Tosatti
Quoted 23 lines · Joerg Roedel

Make use of the KVM_TSC_CONTROL feature if available.

---
 target-i386/kvm.c |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)
target-i386/kvm.c +1 -0
index 10fb2c4..923d2d5 100644
@@ -354,6 +354,7 @@ int kvm_arch_init_vcpu(CPUState *env)
     uint32_t unused;
     struct kvm_cpuid_entry2 *c;
     uint32_t signature[3];
+    int r;
 
     env->cpuid_features &= kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX);
 
@@ -499,7 +500,22 @@ int kvm_arch_init_vcpu(CPUState *env)
 
     qemu_add_vm_change_state_handler(cpu_update_state, env);
 
Quoted 16 lines
Quoted 5 lines
target-i386/kvm.c +4 -1
-    return kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
+    r = kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
+    if (r)
+	    return r;
+

target-i386/kvm.c int kvm_arch_init_vcpu(CPUState *env) · line 516

Quoted 10 lines
target-i386/kvm.c +10 -0
+#ifdef KVM_CAP_TSC_CONTROL
+    r = kvm_check_extension(env->kvm_state, KVM_CAP_TSC_CONTROL);
+    if (r && env->tsc_khz) {
+        r = kvm_vcpu_ioctl(env, KVM_SET_TSC_KHZ, env->tsc_khz);
+        if (r < 0) {
+            fprintf(stderr, "KVM_SET_TSC_KHZ failed\n");
+            return r;
+        }
+    }
+#endif

#ifdef times are over, please clean up before pushing upstream.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#15 lore raw
In reply to Joerg Roedel
Quoted 10 lines · Joerg Roedel

Hi Avi, Marcelo,

here is v2 of the patches to support setting the guests tsc-frequency from the qemu command line. This version addresses the comment from Avi on the first version. To reflect that units can be given to the frequency, the parameter was renamed from tsc_khz to tsc_freq.

Thanks,

Joerg

Applied to uq/master, thanks.