From: "Jan Beulich" Subject: [PATCH] x86: reduce redundancy in tsc_[gs]et_info() Date: Wed, 30 Apr 2014 14:53:01 +0100 Message-ID: <53611C5D020000780000DCAC@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part7745482D.0__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WfUwc-00005C-E5 for xen-devel@lists.xenproject.org; Wed, 30 Apr 2014 13:53:06 +0000 List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Keir Fraser List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part7745482D.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline - some of the case statements are effectively or mostly special cases of others, so there's no good reason not to share the code - in the "get" function, a variable can be made case-wide instead of having multiple instance of it (and those even with a pointless initializer) - minor formatting adjustments Signed-off-by: Jan Beulich --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -1786,26 +1786,22 @@ void tsc_get_info(struct domain *d, uint =20 switch ( *tsc_mode ) { + uint64_t tsc; + case TSC_MODE_NEVER_EMULATE: - *elapsed_nsec =3D *gtsc_khz =3D 0; + *elapsed_nsec =3D *gtsc_khz =3D 0; break; - case TSC_MODE_ALWAYS_EMULATE: - *elapsed_nsec =3D get_s_time() - d->arch.vtsc_offset; - *gtsc_khz =3D d->arch.tsc_khz; - break; case TSC_MODE_DEFAULT: if ( d->arch.vtsc ) { + case TSC_MODE_ALWAYS_EMULATE: *elapsed_nsec =3D get_s_time() - d->arch.vtsc_offset; *gtsc_khz =3D d->arch.tsc_khz; + break; } - else - { - uint64_t tsc =3D 0; - rdtscll(tsc); - *elapsed_nsec =3D scale_delta(tsc,&d->arch.vtsc_to_ns); - *gtsc_khz =3D cpu_khz; - } + rdtscll(tsc); + *elapsed_nsec =3D scale_delta(tsc, &d->arch.vtsc_to_ns); + *gtsc_khz =3D cpu_khz; break; case TSC_MODE_PVRDTSCP: if ( d->arch.vtsc ) @@ -1815,10 +1811,9 @@ void tsc_get_info(struct domain *d, uint } else { - uint64_t tsc =3D 0; rdtscll(tsc); - *elapsed_nsec =3D (scale_delta(tsc,&d->arch.vtsc_to_ns) - - d->arch.vtsc_offset); + *elapsed_nsec =3D scale_delta(tsc, &d->arch.vtsc_to_ns) - + d->arch.vtsc_offset; *gtsc_khz =3D 0; /* ignored by tsc_set_info */ } break; @@ -1875,28 +1870,24 @@ void tsc_set_info(struct domain *d, =20 switch ( d->arch.tsc_mode =3D tsc_mode ) { - case TSC_MODE_NEVER_EMULATE: - d->arch.vtsc =3D 0; - break; - case TSC_MODE_ALWAYS_EMULATE: - d->arch.vtsc =3D 1; - d->arch.vtsc_offset =3D get_s_time() - elapsed_nsec; - d->arch.tsc_khz =3D gtsc_khz ? gtsc_khz : cpu_khz; - set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000 ); - d->arch.ns_to_vtsc =3D scale_reciprocal(d->arch.vtsc_to_ns); - break; case TSC_MODE_DEFAULT: - d->arch.vtsc =3D 1; + case TSC_MODE_ALWAYS_EMULATE: d->arch.vtsc_offset =3D get_s_time() - elapsed_nsec; - d->arch.tsc_khz =3D gtsc_khz ? gtsc_khz : cpu_khz; - set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000 ); - /* use native TSC if initial host has safe TSC, has not migrated - * yet and tsc_khz =3D=3D cpu_khz */ - if ( host_tsc_is_safe() && incarnation =3D=3D 0 && - d->arch.tsc_khz =3D=3D cpu_khz ) + d->arch.tsc_khz =3D gtsc_khz ?: cpu_khz; + set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000); + /* + * In default mode use native TSC if the host has safe TSC, + * the VM has not migrated yet, and tsc_khz =3D=3D cpu_khz. + */ + if ( tsc_mode =3D=3D TSC_MODE_DEFAULT && host_tsc_is_safe() && + incarnation =3D=3D 0 && d->arch.tsc_khz =3D=3D cpu_khz ) + { + case TSC_MODE_NEVER_EMULATE: d->arch.vtsc =3D 0; - else=20 - d->arch.ns_to_vtsc =3D scale_reciprocal(d->arch.vtsc_to_ns); + break; + } + d->arch.vtsc =3D 1; + d->arch.ns_to_vtsc =3D scale_reciprocal(d->arch.vtsc_to_ns); break; case TSC_MODE_PVRDTSCP: d->arch.vtsc =3D boot_cpu_has(X86_FEATURE_RDTSCP) && --=__Part7745482D.0__= Content-Type: text/plain; name="x86-TSC-set-info-cleanup.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-TSC-set-info-cleanup.patch" x86: reduce redundancy in tsc_[gs]et_info()=0A=0A- some of the case = statements are effectively or mostly special cases=0A of others, so = there's no good reason not to share the code=0A- in the "get" function, a = variable can be made case-wide instead of=0A having multiple instance of = it (and those even with a pointless=0A initializer)=0A- minor formatting = adjustments=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/xen/arch/x86/time.c=0A+++ b/xen/arch/x86/time.c=0A@@ -1786,26 +1786,22 = @@ void tsc_get_info(struct domain *d, uint=0A =0A switch ( *tsc_mode = )=0A {=0A+ uint64_t tsc;=0A+=0A case TSC_MODE_NEVER_EMULATE:= =0A- *elapsed_nsec =3D *gtsc_khz =3D 0;=0A+ *elapsed_nsec = =3D *gtsc_khz =3D 0;=0A break;=0A- case TSC_MODE_ALWAYS_EMULATE:= =0A- *elapsed_nsec =3D get_s_time() - d->arch.vtsc_offset;=0A- = *gtsc_khz =3D d->arch.tsc_khz;=0A- break;=0A case TSC_MODE_D= EFAULT:=0A if ( d->arch.vtsc )=0A {=0A+ case TSC_MODE_AL= WAYS_EMULATE:=0A *elapsed_nsec =3D get_s_time() - d->arch.vtsc_= offset;=0A *gtsc_khz =3D d->arch.tsc_khz;=0A+ = break;=0A }=0A- else=0A- {=0A- uint64_t = tsc =3D 0;=0A- rdtscll(tsc);=0A- *elapsed_nsec =3D = scale_delta(tsc,&d->arch.vtsc_to_ns);=0A- *gtsc_khz =3D = cpu_khz;=0A- }=0A+ rdtscll(tsc);=0A+ *elapsed_nsec = =3D scale_delta(tsc, &d->arch.vtsc_to_ns);=0A+ *gtsc_khz =3D = cpu_khz;=0A break;=0A case TSC_MODE_PVRDTSCP:=0A if ( = d->arch.vtsc )=0A@@ -1815,10 +1811,9 @@ void tsc_get_info(struct domain = *d, uint=0A }=0A else=0A {=0A- uint64_t = tsc =3D 0;=0A rdtscll(tsc);=0A- *elapsed_nsec =3D = (scale_delta(tsc,&d->arch.vtsc_to_ns) -=0A- = d->arch.vtsc_offset);=0A+ *elapsed_nsec =3D scale_delta(tsc, = &d->arch.vtsc_to_ns) -=0A+ d->arch.vtsc_offset;= =0A *gtsc_khz =3D 0; /* ignored by tsc_set_info */=0A = }=0A break;=0A@@ -1875,28 +1870,24 @@ void tsc_set_info(struct = domain *d,=0A =0A switch ( d->arch.tsc_mode =3D tsc_mode )=0A = {=0A- case TSC_MODE_NEVER_EMULATE:=0A- d->arch.vtsc =3D 0;=0A- = break;=0A- case TSC_MODE_ALWAYS_EMULATE:=0A- d->arch.vtsc = =3D 1;=0A- d->arch.vtsc_offset =3D get_s_time() - elapsed_nsec;=0A- = d->arch.tsc_khz =3D gtsc_khz ? gtsc_khz : cpu_khz;=0A- = set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000 );=0A- = d->arch.ns_to_vtsc =3D scale_reciprocal(d->arch.vtsc_to_ns);=0A- = break;=0A case TSC_MODE_DEFAULT:=0A- d->arch.vtsc =3D 1;=0A+ = case TSC_MODE_ALWAYS_EMULATE:=0A d->arch.vtsc_offset =3D get_s_time= () - elapsed_nsec;=0A- d->arch.tsc_khz =3D gtsc_khz ? gtsc_khz : = cpu_khz;=0A- set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * = 1000 );=0A- /* use native TSC if initial host has safe TSC, has not = migrated=0A- * yet and tsc_khz =3D=3D cpu_khz */=0A- if ( = host_tsc_is_safe() && incarnation =3D=3D 0 &&=0A- d->arch.ts= c_khz =3D=3D cpu_khz )=0A+ d->arch.tsc_khz =3D gtsc_khz ?: = cpu_khz;=0A+ set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * = 1000);=0A+ /*=0A+ * In default mode use native TSC if the = host has safe TSC,=0A+ * the VM has not migrated yet, and tsc_khz = =3D=3D cpu_khz.=0A+ */=0A+ if ( tsc_mode =3D=3D TSC_MODE_DEF= AULT && host_tsc_is_safe() &&=0A+ incarnation =3D=3D 0 && = d->arch.tsc_khz =3D=3D cpu_khz )=0A+ {=0A+ case TSC_MODE_NEVER_EM= ULATE:=0A d->arch.vtsc =3D 0;=0A- else =0A- = d->arch.ns_to_vtsc =3D scale_reciprocal(d->arch.vtsc_to_ns);=0A+ = break;=0A+ }=0A+ d->arch.vtsc =3D 1;=0A+ d->arch.ns_t= o_vtsc =3D scale_reciprocal(d->arch.vtsc_to_ns);=0A break;=0A = case TSC_MODE_PVRDTSCP:=0A d->arch.vtsc =3D boot_cpu_has(X86_FEATU= RE_RDTSCP) &&=0A --=__Part7745482D.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part7745482D.0__=--