From: "Jan Beulich" Subject: [PATCH v2] x86: reduce redundancy in tsc_[gs]et_info() Date: Tue, 06 May 2014 14:29:20 +0100 Message-ID: <5368FFD0020000780000F64D@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part102226A0.0__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WhfQz-0001oa-84 for xen-devel@lists.xenproject.org; Tue, 06 May 2014 13:29:25 +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: Boris Ostrovsky , 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. --=__Part102226A0.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 Reviewed-by: Boris Ostrovsky --- v2: A few more formatting adjustments. Re-base on top of 82713ec8 ("x86: use native RDTSC(P) execution when guest and host frequencies are the same"), replacing that commit's outer || expression with a ?: one, thus better matching the comment (no functional change as incarnation =3D=3D 0 implies d->arch.tsc_khz =3D=3D cpu_khz). --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -1794,39 +1794,34 @@ 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; - } - else - { - uint64_t tsc =3D 0; - rdtscll(tsc); - *elapsed_nsec =3D scale_delta(tsc,&d->arch.vtsc_to_ns); - *gtsc_khz =3D cpu_khz; + *gtsc_khz =3D d->arch.tsc_khz; + break; } + 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 ) { *elapsed_nsec =3D get_s_time() - d->arch.vtsc_offset; - *gtsc_khz =3D cpu_khz; + *gtsc_khz =3D cpu_khz; } 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; @@ -1883,38 +1878,32 @@ 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 ); + d->arch.tsc_khz =3D gtsc_khz ?: cpu_khz; + set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000); /* - * Use native TSC if the host has safe TSC and: + * In default mode use native TSC if the host has safe TSC and: * HVM/PVH: host and guest frequencies are the same (either * "naturally" or via TSC scaling) * PV: guest has not migrated yet (and thus arch.tsc_khz =3D=3D = cpu_khz) */ - if ( host_tsc_is_safe() && - ((has_hvm_container_domain(d) && - (d->arch.tsc_khz =3D=3D cpu_khz || cpu_has_tsc_ratio)) || + if ( tsc_mode =3D=3D TSC_MODE_DEFAULT && host_tsc_is_safe() && + (has_hvm_container_domain(d) ? + d->arch.tsc_khz =3D=3D cpu_khz || cpu_has_tsc_ratio : incarnation =3D=3D 0) ) + { + 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) && - host_tsc_is_safe() ? 0 : 1; + d->arch.vtsc =3D boot_cpu_has(X86_FEATURE_RDTSCP) && + host_tsc_is_safe() ? 0 : 1; d->arch.tsc_khz =3D 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); --=__Part102226A0.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 =0AReviewed-= by: Boris Ostrovsky =0A---=0Av2: A few more = formatting adjustments. Re-base on top of 82713ec8=0A ("x86: use native = RDTSC(P) execution when guest and host=0A frequencies are the same"), = replacing that commit's outer ||=0A expression with a ?: one, thus = better matching the comment (no=0A functional change as incarnation = =3D=3D 0 implies=0A d->arch.tsc_khz =3D=3D cpu_khz).=0A=0A--- a/xen/arch= /x86/time.c=0A+++ b/xen/arch/x86/time.c=0A@@ -1794,39 +1794,34 @@ 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_DEF= AULT:=0A if ( d->arch.vtsc )=0A {=0A+ case TSC_MODE_ALWA= YS_EMULATE:=0A *elapsed_nsec =3D get_s_time() - d->arch.vtsc_of= fset;=0A- *gtsc_khz =3D d->arch.tsc_khz;=0A- }=0A- = else=0A- {=0A- uint64_t tsc =3D 0;=0A- = rdtscll(tsc);=0A- *elapsed_nsec =3D scale_delta(tsc,&d->arch.vts= c_to_ns);=0A- *gtsc_khz =3D cpu_khz;=0A+ *gtsc_khz = =3D d->arch.tsc_khz;=0A+ break;=0A }=0A+ = rdtscll(tsc);=0A+ *elapsed_nsec =3D scale_delta(tsc, &d->arch.vtsc_t= o_ns);=0A+ *gtsc_khz =3D cpu_khz;=0A break;=0A case = TSC_MODE_PVRDTSCP:=0A if ( d->arch.vtsc )=0A {=0A = *elapsed_nsec =3D get_s_time() - d->arch.vtsc_offset;=0A- = *gtsc_khz =3D cpu_khz;=0A+ *gtsc_khz =3D cpu_khz;=0A = }=0A else=0A {=0A- uint64_t tsc =3D 0;=0A = rdtscll(tsc);=0A- *elapsed_nsec =3D (scale_delta(tsc,&d->a= rch.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@@ = -1883,38 +1878,32 @@ void tsc_set_info(struct domain *d,=0A =0A switch = ( d->arch.tsc_mode =3D tsc_mode )=0A {=0A- case TSC_MODE_NEVER_EMULA= TE:=0A- d->arch.vtsc =3D 0;=0A- break;=0A- case TSC_MODE_A= LWAYS_EMULATE:=0A- d->arch.vtsc =3D 1;=0A- d->arch.vtsc_offse= t =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_reciproca= l(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+ 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- * Use native TSC if the = host has safe TSC and:=0A+ * In default mode use native TSC if the = host has safe TSC and:=0A * HVM/PVH: host and guest frequencies = are the same (either=0A * "naturally" or via TSC = scaling)=0A * PV: guest has not migrated yet (and thus arch.tsc_k= hz =3D=3D cpu_khz)=0A */=0A- if ( host_tsc_is_safe() = &&=0A- ((has_hvm_container_domain(d) &&=0A- = (d->arch.tsc_khz =3D=3D cpu_khz || cpu_has_tsc_ratio)) ||=0A+ if ( = tsc_mode =3D=3D TSC_MODE_DEFAULT && host_tsc_is_safe() &&=0A+ = (has_hvm_container_domain(d) ?=0A+ d->arch.tsc_khz =3D=3D = cpu_khz || cpu_has_tsc_ratio :=0A incarnation =3D=3D 0) = )=0A+ {=0A+ case TSC_MODE_NEVER_EMULATE:=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_to_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_FEATURE_RD= TSCP) &&=0A- host_tsc_is_safe() ? 0 : 1;=0A+ = d->arch.vtsc =3D boot_cpu_has(X86_FEATURE_RDTSCP) &&=0A+ = host_tsc_is_safe() ? 0 : 1;=0A d->arch.tsc_khz =3D = 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 --=__Part102226A0.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 --=__Part102226A0.0__=--