From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by mx.groups.io with SMTP id smtpd.web08.4847.1607683752111898336 for ; Fri, 11 Dec 2020 02:49:12 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.115, mailfrom: ray.ni@intel.com) IronPort-SDR: EiTV51W0H2ETMaRceADIuYj9shECIy88CP1ohxHSxAQmFykqqGmULEiErUmQpLpZUDbSS6FHqQ bIV8UNzGbQdQ== X-IronPort-AV: E=McAfee;i="6000,8403,9831"; a="173648922" X-IronPort-AV: E=Sophos;i="5.78,411,1599548400"; d="scan'208";a="173648922" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Dec 2020 02:49:11 -0800 IronPort-SDR: uELjAe85P+I9zXZKxyLz7RljRU6Gpsl0O2cjMYOUsNYTGCLx3WNQl5F1xlVx8aTrg93lm7JsVu 5QG2Tuvn7H+w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.78,411,1599548400"; d="scan'208";a="409155096" Received: from ray-dev.ccr.corp.intel.com ([10.239.158.87]) by orsmga001.jf.intel.com with ESMTP; 11 Dec 2020 02:49:10 -0800 From: "Ni, Ray" To: devel@edk2.groups.io Cc: Eric Dong , Star Zeng , Yun Lou , Laszlo Ersek Subject: [PATCH V2] UefiCpuPkg/CpuFeature: reduce time complexty to calc CpuInfo.First Date: Fri, 11 Dec 2020 18:47:39 +0800 Message-Id: <20201211104739.172-1-ray.ni@intel.com> X-Mailer: git-send-email 2.27.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable CpuInfo.First stores whether the current thread belongs to the first package in the platform, first core in a package, first thread in a core. But the time complexity of original algorithm to calculate the CpuInfo.First is O (n) * O (p) * O (c). n: number of processors p: number of packages c: number of cores per package The patch trades time with space by storing the first package, first core per package, first thread per core in an array. The time complexity becomes O (n). Signed-off-by: Ray Ni Cc: Eric Dong Cc: Star Zeng Cc: Yun Lou Cc: Laszlo Ersek --- .../CpuFeaturesInitialize.c | 96 +++++++++---------- 1 file changed, 47 insertions(+), 49 deletions(-) diff --git a/UefiCpuPkg/Library/RegisterCpuFeaturesLib/CpuFeaturesInitializ= e.c b/UefiCpuPkg/Library/RegisterCpuFeaturesLib/CpuFeaturesInitialize.c index d4a576385f..a1e972b1a2 100644 --- a/UefiCpuPkg/Library/RegisterCpuFeaturesLib/CpuFeaturesInitialize.c +++ b/UefiCpuPkg/Library/RegisterCpuFeaturesLib/CpuFeaturesInitialize.c @@ -105,7 +105,10 @@ CpuInitDataInitialize ( EFI_CPU_PHYSICAL_LOCATION *Location;=0D UINT32 PackageIndex;=0D UINT32 CoreIndex;=0D - UINT32 First;=0D + UINTN Pages;=0D + UINT32 FirstPackage;=0D + UINT32 *FirstCore;=0D + UINT32 *FirstThread;=0D ACPI_CPU_DATA *AcpiCpuData;=0D CPU_STATUS_INFORMATION *CpuStatus;=0D UINT32 *ThreadCountPerPackage;=0D @@ -236,74 +239,69 @@ CpuInitDataInitialize ( =0D //=0D // Initialize CpuFeaturesData->InitOrder[].CpuInfo.First=0D + // Use AllocatePages () instead of AllocatePool () because pool cannot b= e freed in PEI phase but page can.=0D //=0D + Pages =3D EFI_SIZE_TO_PAGES (CpuStatus->PackageCount * sizeof (UINT3= 2) + CpuStatus->PackageCount * CpuStatus->MaxCoreCount * sizeof (UINT32));= =0D + FirstCore =3D AllocatePages (Pages);=0D + ASSERT (FirstCore !=3D NULL);=0D + FirstThread =3D FirstCore + CpuStatus->PackageCount;=0D =0D //=0D - // Set First.Package for each thread belonging to the first package.=0D + // Set FirstPackage, FirstCore[], FirstThread[] to maximum package ID, c= ore ID, thread ID.=0D //=0D - First =3D MAX_UINT32;=0D + FirstPackage =3D MAX_UINT32;=0D + SetMem32 (FirstCore, CpuStatus->PackageCount * sizeof (UINT32), MAX_UI= NT32);=0D + SetMem32 (FirstThread, CpuStatus->PackageCount * CpuStatus->MaxCoreCount= * sizeof (UINT32), MAX_UINT32);=0D +=0D for (ProcessorNumber =3D 0; ProcessorNumber < NumberOfCpus; ProcessorNum= ber++) {=0D Location =3D &CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.Proc= essorInfo.Location;=0D - First =3D MIN (Location->Package, First);=0D +=0D + //=0D + // Save the minimum package ID in the platform.=0D + //=0D + FirstPackage =3D MIN (Location->Package, FirstPackage)= ;=0D + =0D + //=0D + // Save the minimum core ID per package.=0D + //=0D + FirstCore[Location->Package] =3D MIN (Location->Core, FirstCore[Locati= on->Package]);=0D + =0D + //=0D + // Save the minimum thread ID per core.=0D + //=0D + FirstThread[Location->Package * CpuStatus->MaxCoreCount + Location->Co= re] =3D MIN (=0D + Location->Thread,=0D + FirstThread[Location->Package * CpuStatus->MaxCoreCount + Location->= Core]=0D + );=0D }=0D +=0D + //=0D + // Update the First field.=0D + //=0D for (ProcessorNumber =3D 0; ProcessorNumber < NumberOfCpus; ProcessorNum= ber++) {=0D Location =3D &CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.Proc= essorInfo.Location;=0D - if (Location->Package =3D=3D First) {=0D +=0D + if (Location->Package =3D=3D FirstPackage) {=0D CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.First.Package = =3D 1;=0D }=0D - }=0D =0D - //=0D - // Set First.Die/Tile/Module for each thread assuming:=0D - // single Die under each package, single Tile under each Die, single Mo= dule under each Tile=0D - //=0D - for (ProcessorNumber =3D 0; ProcessorNumber < NumberOfCpus; ProcessorNum= ber++) {=0D + //=0D + // Set First.Die/Tile/Module for each thread assuming:=0D + // single Die under each package, single Tile under each Die, single = Module under each Tile=0D + //=0D CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.First.Die =3D 1;=0D CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.First.Tile =3D 1;= =0D CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.First.Module =3D 1= ;=0D - }=0D =0D - for (PackageIndex =3D 0; PackageIndex < CpuStatus->PackageCount; Package= Index++) {=0D - //=0D - // Set First.Core for each thread in the first core of each package.=0D - //=0D - First =3D MAX_UINT32;=0D - for (ProcessorNumber =3D 0; ProcessorNumber < NumberOfCpus; ProcessorN= umber++) {=0D - Location =3D &CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.Pr= ocessorInfo.Location;=0D - if (Location->Package =3D=3D PackageIndex) {=0D - First =3D MIN (Location->Core, First);=0D - }=0D + if (Location->Core =3D=3D FirstCore[Location->Package]) {=0D + CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.First.Core =3D 1= ;=0D }=0D -=0D - for (ProcessorNumber =3D 0; ProcessorNumber < NumberOfCpus; ProcessorN= umber++) {=0D - Location =3D &CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.Pr= ocessorInfo.Location;=0D - if (Location->Package =3D=3D PackageIndex && Location->Core =3D=3D F= irst) {=0D - CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.First.Core =3D= 1;=0D - }=0D + if (Location->Thread =3D=3D FirstThread[Location->Package * CpuStatus-= >MaxCoreCount + Location->Core]) {=0D + CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.First.Thread =3D= 1;=0D }=0D }=0D =0D - for (PackageIndex =3D 0; PackageIndex < CpuStatus->PackageCount; Package= Index++) {=0D - for (CoreIndex =3D 0; CoreIndex < CpuStatus->MaxCoreCount; CoreIndex++= ) {=0D - //=0D - // Set First.Thread for the first thread of each core.=0D - //=0D - First =3D MAX_UINT32;=0D - for (ProcessorNumber =3D 0; ProcessorNumber < NumberOfCpus; Processo= rNumber++) {=0D - Location =3D &CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.= ProcessorInfo.Location;=0D - if (Location->Package =3D=3D PackageIndex && Location->Core =3D=3D= CoreIndex) {=0D - First =3D MIN (Location->Thread, First);=0D - }=0D - }=0D -=0D - for (ProcessorNumber =3D 0; ProcessorNumber < NumberOfCpus; Processo= rNumber++) {=0D - Location =3D &CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.= ProcessorInfo.Location;=0D - if (Location->Package =3D=3D PackageIndex && Location->Core =3D=3D= CoreIndex && Location->Thread =3D=3D First) {=0D - CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo.First.Thread= =3D 1;=0D - }=0D - }=0D - }=0D - }=0D + FreePages (FirstCore, Pages);=0D }=0D =0D /**=0D --=20 2.27.0.windows.1