From 1e24b99534002a00a69180e5412a59e239c2f896 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 11 Aug 2020 17:58:41 +0800 Subject: [PATCH] TD-1057 system function in windows --- src/os/src/windows/w64Atomic.c | 4 ++ src/os/src/windows/w64Lz4.c | 26 +++++++++- src/os/src/windows/w64Sysinfo.c | 91 +++++++++++++++++++++++++++++++-- 3 files changed, 115 insertions(+), 6 deletions(-) diff --git a/src/os/src/windows/w64Atomic.c b/src/os/src/windows/w64Atomic.c index 0425f4ed3f..9fc3eae672 100644 --- a/src/os/src/windows/w64Atomic.c +++ b/src/os/src/windows/w64Atomic.c @@ -43,7 +43,11 @@ long interlocked_add_fetch_32(long volatile* ptr, long val) { } __int64 interlocked_add_fetch_64(__int64 volatile* ptr, __int64 val) { +#ifdef _WIN64 return _InterlockedExchangeAdd64(ptr, val) + val; +#else + return _InterlockedExchangeAdd(ptr, val) + val; +#endif } // and diff --git a/src/os/src/windows/w64Lz4.c b/src/os/src/windows/w64Lz4.c index 96556c1f1c..631a22e572 100644 --- a/src/os/src/windows/w64Lz4.c +++ b/src/os/src/windows/w64Lz4.c @@ -21,9 +21,29 @@ #include "tulog.h" #include "tutil.h" +unsigned char _MyBitScanForward64(unsigned long *ret, uint64_t x) { + unsigned long x0 = (unsigned long)x, top, bottom; + _BitScanForward(&top, (unsigned long)(x >> 32)); + _BitScanForward(&bottom, x0); + *ret = x0 ? bottom : 32 + top; + return x != 0; +} + +unsigned char _MyBitScanReverse64(unsigned long *ret, uint64_t x) { + unsigned long x1 = (unsigned long)(x >> 32), top, bottom; + _BitScanReverse(&top, x1); + _BitScanReverse(&bottom, (unsigned long)x); + *ret = x1 ? top + 32 : bottom; + return x != 0; +} + int32_t BUILDIN_CLZL(uint64_t val) { unsigned long r = 0; +#ifdef _WIN64 _BitScanReverse64(&r, val); +#else + _MyBitScanReverse64(&r, val); +#endif return (int)(r >> 3); } @@ -35,7 +55,11 @@ int32_t BUILDIN_CLZ(uint32_t val) { int32_t BUILDIN_CTZL(uint64_t val) { unsigned long r = 0; +#ifdef _WIN64 _BitScanForward64(&r, val); +#else + _MyBitScanForward64(&r, val); +#endif return (int)(r >> 3); } @@ -43,4 +67,4 @@ int32_t BUILDIN_CTZ(uint32_t val) { unsigned long r = 0; _BitScanForward(&r, val); return (int)(r >> 3); -} +} \ No newline at end of file diff --git a/src/os/src/windows/w64Sysinfo.c b/src/os/src/windows/w64Sysinfo.c index 27869e1eec..70abe939b2 100644 --- a/src/os/src/windows/w64Sysinfo.c +++ b/src/os/src/windows/w64Sysinfo.c @@ -21,6 +21,15 @@ #include "ttimer.h" #include "tulog.h" #include "tutil.h" +#if (_WIN64) +#include +#include +#include +#include +#include +#include +#pragma comment(lib, "Mswsock.lib ") +#endif static void taosGetSystemTimezone() { // get and set default timezone @@ -69,11 +78,64 @@ void taosGetSystemInfo() { taosGetSystemLocale(); } -bool taosGetDisk() { return true; } +bool taosGetDisk() { + const double unit = 1024 * 1024 * 1024; + BOOL fResult; + unsigned _int64 i64FreeBytesToCaller; + unsigned _int64 i64TotalBytes; + unsigned _int64 i64FreeBytes; + char dir[4] = {'C', ':', '\\', '\0'}; + int drive_type; + + if (tscEmbedded) { + drive_type = GetDriveTypeA(dir); + if (drive_type == DRIVE_FIXED) { + fResult = GetDiskFreeSpaceExA(dir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, + (PULARGE_INTEGER)&i64FreeBytes); + if (fResult) { + tsTotalDataDirGB = tsTotalLogDirGB = tsTotalTmpDirGB = (float)(i64TotalBytes / unit); + tsAvailDataDirGB = tsAvailLogDirGB = tsAvailTmpDirectorySpace = (float)(i64FreeBytes / unit); + } + } + } + return true; +} + +bool taosReadProcIO(int64_t *readbyte, int64_t *writebyte) { + IO_COUNTERS io_counter; + if (GetProcessIoCounters(GetCurrentProcess(), &io_counter)) { + if (readbyte) *readbyte = io_counter.ReadTransferCount; + if (writebyte) *writebyte = io_counter.WriteTransferCount; + return true; + } + return false; +} bool taosGetProcIO(float *readKB, float *writeKB) { - *readKB = 0; - *writeKB = 0; + static int64_t lastReadbyte = -1; + static int64_t lastWritebyte = -1; + + int64_t curReadbyte = 0; + int64_t curWritebyte = 0; + + if (!taosReadProcIO(&curReadbyte, &curWritebyte)) { + return false; + } + + if (lastReadbyte == -1 || lastWritebyte == -1) { + lastReadbyte = curReadbyte; + lastWritebyte = curWritebyte; + return false; + } + + *readKB = (float)((double)(curReadbyte - lastReadbyte) / 1024); + *writeKB = (float)((double)(curWritebyte - lastWritebyte) / 1024); + if (*readKB < 0) *readKB = 0; + if (*writeKB < 0) *writeKB = 0; + + lastReadbyte = curReadbyte; + lastWritebyte = curWritebyte; + return true; } @@ -89,12 +151,31 @@ bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { } bool taosGetProcMemory(float *memoryUsedMB) { - *memoryUsedMB = 0; + unsigned bytes_used = 0; +#if defined(_WIN32) && defined(_MSC_VER) + PROCESS_MEMORY_COUNTERS pmc; + HANDLE cur_proc = GetCurrentProcess(); + + if (GetProcessMemoryInfo(cur_proc, &pmc, sizeof(pmc))) { + bytes_used = (unsigned)(pmc.WorkingSetSize + pmc.PagefileUsage); + } +#endif + + *memoryUsedMB = (float)bytes_used / 1024 / 1024; + return true; } bool taosGetSysMemory(float *memoryUsedMB) { - *memoryUsedMB = 0; + MEMORYSTATUSEX memsStat; + float nMemFree; + float nMemTotal; + + memsStat.dwLength = sizeof(memsStat); + if (!GlobalMemoryStatusEx(&memsStat)) { return false; } + nMemFree = memsStat.ullAvailPhys / (1024.0f * 1024.0f); + nMemTotal = memsStat.ullTotalPhys / (1024.0f * 1024.0f); + *memoryUsedMB = nMemTotal - nMemFree; return true; } -- GitLab