From 012d84cc5ceb588aae36275b97b0417895fc06ba Mon Sep 17 00:00:00 2001 From: Gengchen Tuo Date: Wed, 22 Nov 2023 10:31:42 -0500 Subject: [PATCH] Return another error if only one data point has been recorded This is to determine if a call to omrsysinfo_get_CPU_load has only one data point recorded. Also fixed a mistake in the description saying that the first two invocations return OMRPORT_ERROR_OPFAILED and fix the time unit in the description to "ms". Related: https://github.com/eclipse-openj9/openj9/pull/18451 Signed-off-by: Gengchen Tuo --- fvtest/porttest/si.cpp | 14 ++++++-------- include_core/omrporterror.h | 1 + port/common/omrsysinfo.c | 8 ++++---- port/unix/omrsysinfo.c | 4 ++-- port/win32/omrsysinfo.c | 2 +- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/fvtest/porttest/si.cpp b/fvtest/porttest/si.cpp index 41b24e98dc..b8eafbcfd1 100644 --- a/fvtest/porttest/si.cpp +++ b/fvtest/porttest/si.cpp @@ -1690,19 +1690,17 @@ TEST(PortSysinfoTest, sysinfo_test_get_CPU_load) { OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary()); - /* As per the API specification the first two calls to this API will return a negative portable error code. However - * for the purposes of this test we will not be testing this. This is because the test infrastructure is setup such - * that we cannot guarantee that no other test has called omrsysinfo_get_CPU_utlization or omrsysinfo_get_CPU_load - * up to this point. If some other test did call these APIs then the internal buffers would have been populated and - * as such the omrsysinfo_get_CPU_load could return a zero return code on the very first invocation within this - * test. + /* As per the API specification if only one data point has been recorded this API will return a negative portable + * error code. However for the purposes of this test we will not be testing this. This is because the test infrastructure + * is setup such that we cannot guarantee that no other test has called omrsysinfo_get_CPU_load up to this point. If + * some other test calls this API then the internal buffers would have been populated and as such the omrsysinfo_get_CPU_load + * could return a zero return code on the very first invocation within this test. * - * To avoid inter-test dependencies we do not assert on the return value of the first two calls here, and only test + * To avoid inter-test dependencies we do not assert on the return value of the first call here, and only test * that the API returns valid numbers within the range outlined in the API specification. */ double cpuLoad; omrsysinfo_get_CPU_load(&cpuLoad); - omrsysinfo_get_CPU_load(&cpuLoad); /* Sleep for 100ms before re-sampling processor usage stats. This allows other processes and the operating system to * use the CPU and drive up the user and kernel utilization. The call to cpuBurner probably won't be optimized out, diff --git a/include_core/omrporterror.h b/include_core/omrporterror.h index feeeee5ba0..c5e8fb0ad3 100644 --- a/include_core/omrporterror.h +++ b/include_core/omrporterror.h @@ -67,6 +67,7 @@ #define OMRPORT_ERROR_INVALID_HANDLE (OMRPORT_ERROR_BASE-17) #define OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM (OMRPORT_ERROR_BASE-18) #define OMRPORT_ERROR_INVALID_ARGUMENTS (OMRPORT_ERROR_BASE-19) +#define OMRPORT_ERROR_INSUFFICIENT_DATA (OMRPORT_ERROR_BASE-20) /** @} */ /** diff --git a/port/common/omrsysinfo.c b/port/common/omrsysinfo.c index 02e1beda9c..5f942713ed 100644 --- a/port/common/omrsysinfo.c +++ b/port/common/omrsysinfo.c @@ -651,10 +651,10 @@ omrsysinfo_get_CPU_utilization(struct OMRPortLibrary *portLibrary, struct J9Sysi * @param[out] cpuLoad the cumulative CPU utilization of all CPUs on the system * * @return - * - 0 on success - * - \arg OMRPORT_ERROR_OPFAILED on the first two invocations of this API - * - \arg OMRPORT_ERROR_OPFAILED if less than 10ns have passed since the second call to this API - * - negative portable error code on other failures + * \arg 0 on success + * \arg OMRPORT_ERROR_INSUFFICIENT_DATA if only one data point has been recorded + * \arg OMRPORT_ERROR_OPFAILED if less than 10 ms have passed since the last call to this API + * \arg negative portable error code on other failures */ intptr_t omrsysinfo_get_CPU_load(struct OMRPortLibrary *portLibrary, double *cpuLoad) diff --git a/port/unix/omrsysinfo.c b/port/unix/omrsysinfo.c index 2fa15bd651..5b0832320a 100644 --- a/port/unix/omrsysinfo.c +++ b/port/unix/omrsysinfo.c @@ -4642,7 +4642,7 @@ omrsysinfo_get_CPU_load(struct OMRPortLibrary *portLibrary, double *cpuLoad) if (oldestCPUTime->timestamp == 0) { *oldestCPUTime = currentCPUTime; *latestCPUTime = currentCPUTime; - return OMRPORT_ERROR_OPFAILED; + return OMRPORT_ERROR_INSUFFICIENT_DATA; } /* Calculate using the most recent value in the history */ @@ -4674,7 +4674,7 @@ omrsysinfo_get_CPU_load(struct OMRPortLibrary *portLibrary, double *cpuLoad) if (oldestCPUTime->timestamp == 0) { *oldestCPUTime = currentCPUTime; *latestCPUTime = currentCPUTime; - return OMRPORT_ERROR_OPFAILED; + return OMRPORT_ERROR_INSUFFICIENT_DATA; } /* Calculate using the most recent value in the history */ diff --git a/port/win32/omrsysinfo.c b/port/win32/omrsysinfo.c index fd81f8730f..baf108f15e 100644 --- a/port/win32/omrsysinfo.c +++ b/port/win32/omrsysinfo.c @@ -1271,7 +1271,7 @@ omrsysinfo_get_CPU_load(struct OMRPortLibrary *portLibrary, double *cpuLoad) if (oldestCPUTime->timestamp == 0) { *oldestCPUTime = currentCPUTime; *latestCPUTime = currentCPUTime; - return OMRPORT_ERROR_OPFAILED; + return OMRPORT_ERROR_INSUFFICIENT_DATA; } /* Calculate using the most recent value in the history */