From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.93; helo=mga11.intel.com; envelope-from=eric.dong@intel.com; receiver=edk2-devel@lists.01.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 6DAF520986AAB for ; Wed, 4 Jul 2018 01:37:41 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Jul 2018 01:37:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,306,1526367600"; d="scan'208";a="55032999" Received: from ydong10-win10.ccr.corp.intel.com ([10.239.9.24]) by orsmga006.jf.intel.com with ESMTP; 04 Jul 2018 01:37:38 -0700 From: Eric Dong To: edk2-devel@lists.01.org Cc: Ruiyu Ni , Jeff Fan , Laszlo Ersek Date: Wed, 4 Jul 2018 16:37:36 +0800 Message-Id: <20180704083736.9272-1-eric.dong@intel.com> X-Mailer: git-send-email 2.15.0.windows.1 Subject: [Patch] UefiCpuPkg/MpInitLib: Optimize get processor number performance. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Jul 2018 08:37:41 -0000 Current function has low performance because it calls GetApicId many times. New logic first try to base on the stack range used by AP to find the processor number. If this solution failed, then call GetApicId once and base on this value to search the processor. Cc: Ruiyu Ni Cc: Jeff Fan Cc: Laszlo Ersek Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Eric Dong --- UefiCpuPkg/Library/MpInitLib/MpLib.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpInitLib/MpLib.c index eb2765910c..abd65bee1a 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -418,7 +418,8 @@ ApInitializeSync ( } /** - Find the current Processor number by APIC ID. + First try to find the current Processor number by stack address, + if it failed, then base on APIC ID. @param[in] CpuMpData Pointer to PEI CPU MP Data @param[out] ProcessorNumber Return the pocessor number found @@ -435,16 +436,34 @@ GetProcessorNumber ( UINTN TotalProcessorNumber; UINTN Index; CPU_INFO_IN_HOB *CpuInfoInHob; + UINT32 CurrentApicId; + TotalProcessorNumber = CpuMpData->CpuCount; CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob; - TotalProcessorNumber = CpuMpData->CpuCount; + // + // First try to base on current stack address to find the AP index. + // &TotalProcessorNumber value located in the stack range. + // for (Index = 0; Index < TotalProcessorNumber; Index ++) { - if (CpuInfoInHob[Index].ApicId == GetApicId ()) { + if ((CpuInfoInHob[Index].ApTopOfStack > (UINTN) (&TotalProcessorNumber)) && + (CpuInfoInHob[Index].ApTopOfStack - CpuMpData->CpuApStackSize < (UINTN) (&TotalProcessorNumber))) { *ProcessorNumber = Index; return EFI_SUCCESS; } } + + // + // If can't base on stack to find the AP index, use the APIC ID. + // + CurrentApicId = GetApicId (); + for (Index = 0; Index < TotalProcessorNumber; Index ++) { + if (CpuInfoInHob[Index].ApicId == CurrentApicId) { + *ProcessorNumber = Index; + return EFI_SUCCESS; + } + } + return EFI_NOT_FOUND; } -- 2.15.0.windows.1