From a6c02e29c1e1f2bb810839dc40e3f289cb2de393 Mon Sep 17 00:00:00 2001 From: vcbchang Date: Mon, 2 Aug 2021 11:14:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E5=9C=A8=E4=BD=BF?= =?UTF-8?q?=E7=94=A8GetCputime=E6=97=B6=E7=94=B1clockid=E8=AE=A1=E7=AE=97p?= =?UTF-8?q?id=E6=88=96tid=E6=97=B6=E5=87=BA=E7=8E=B0=E7=9A=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 【背景】原来代码在计算由clockid计算pid或tid时用的是移位的方式,由于对于有符号和无符号数结果可能不一致,故这里对计算方法做了修改,修改为计算clockid的逆运算。 【修改方案】 计算pid或tid为计算clockid的逆运算 re #I4249G Signed-off-by: vcbchang Change-Id: I2952232ec7d8fc2d0c47163b00d0c772648a2bf8 --- compat/posix/src/time.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/compat/posix/src/time.c b/compat/posix/src/time.c index 865f8dbd..42ee50ae 100644 --- a/compat/posix/src/time.c +++ b/compat/posix/src/time.c @@ -452,11 +452,25 @@ int clock_settime(clockid_t clockID, const struct timespec *tp) } #ifdef LOSCFG_KERNEL_CPUP +inline UINT32 GetTidFromClockID(clockid_t clockID) +{ + // In musl/src/thread/pthread_getcpuclockid.c, we know 'clockid = (-tid - 1) * 8 + 6' + UINT32 tid = -(clockID - 6) / 8 - 1; // 6 8 1 inverse operation from clockID to tid + return tid; +} + +inline const pid_t GetPidFromClockID(clockid_t clockID) +{ + // In musl/src/time/clock_getcpuclockid.c, we know 'clockid = (-pid - 1) * 8 + 2' + const pid_t pid = -(clockID - 2) / 8 - 1; // 2 8 1 inverse operation from clockID to pid + return pid; +} + static int PthreadGetCputime(clockid_t clockID, struct timespec *ats) { uint64_t runtime; UINT32 intSave; - UINT32 tid = ((UINT32) ~((UINT32)(clockID) >> CPUCLOCK_ID_OFFSET)); + UINT32 tid = GetTidFromClockID(clockID); if (OS_TID_CHECK_INVALID(tid)) { return -EINVAL; @@ -482,7 +496,7 @@ static int ProcessGetCputime(clockid_t clockID, struct timespec *ats) { UINT64 runtime; UINT32 intSave; - const pid_t pid = ((pid_t) ~((UINT32)(clockID) >> CPUCLOCK_ID_OFFSET)); + const pid_t pid = GetPidFromClockID(clockID); LosProcessCB *spcb = NULL; if (OsProcessIDUserCheckInvalid(pid) || pid < 0) { @@ -524,7 +538,7 @@ static int GetCputime(clockid_t clockID, struct timespec *tp) static int CheckClock(const clockid_t clockID) { int error = 0; - const pid_t pid = ((pid_t) ~((UINT32)(clockID) >> CPUCLOCK_ID_OFFSET)); + const pid_t pid = GetPidFromClockID(clockID); if (!((UINT32)clockID & CPUCLOCK_PERTHREAD_MASK)) { LosProcessCB *spcb = NULL; -- GitLab