diff --git a/microscript/ILibDuktape_fs.c b/microscript/ILibDuktape_fs.c index 5468577..0dcac29 100644 --- a/microscript/ILibDuktape_fs.c +++ b/microscript/ILibDuktape_fs.c @@ -2590,7 +2590,9 @@ void ILibDuktape_fs_PUSH(duk_context *ctx, void *chain) exports.statSync = function statSync(pathstr) { return(this._statSync(this._fixwinpath(pathstr))); };\ exports.readdirSync = function readdirSync(pathstr)\ {\ + var sysnative=false;\ pathstr = exports._fixwinpath(pathstr);\ + if(require('os').arch()=='x64' && require('_GenericMarshal').PointerSize==4 && pathstr.split('\\\\*').join('').toLowerCase()==process.env['windir'].toLowerCase()) { sysnative=true; }\ if(!pathstr.endsWith('*'))\ {\ if(pathstr.endsWith('\\\\'))\ @@ -2602,7 +2604,9 @@ void ILibDuktape_fs_PUSH(duk_context *ctx, void *chain) pathstr += '\\\\*';\ }\ }\ - return(exports._readdirSync(pathstr));\ + var ret = exports._readdirSync(pathstr);\ + if(sysnative) { ret.push('sysnative'); }\ + return(ret);\ };\ }"; ILibDuktape_ModSearch_AddHandler_AlsoIncludeJS(ctx, copyFile, sizeof(copyFile) - 1); diff --git a/modules/sysinfo.js b/modules/sysinfo.js index 02c355d..d987e18 100644 --- a/modules/sysinfo.js +++ b/modules/sysinfo.js @@ -14,6 +14,12 @@ See the License for the specific language governing permissions and limitations under the License. */ + +// +// sysinfo is a helper module used to query various system metrics in a platform agnostic fashion +// + + var PDH_FMT_LONG = 0x00000100; var PDH_FMT_DOUBLE = 0x00000200; @@ -22,18 +28,21 @@ if (process.platform == 'win32') { var GM = require('_GenericMarshal'); GM.kernel32 = GM.CreateNativeProxy('kernel32.dll'); - GM.kernel32.CreateMethod('GlobalMemoryStatusEx'); + GM.kernel32.CreateMethod('GlobalMemoryStatusEx'); // https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex GM.pdh = GM.CreateNativeProxy('pdh.dll'); - GM.pdh.CreateMethod('PdhAddEnglishCounterA'); - GM.pdh.CreateMethod('PdhCloseQuery'); - GM.pdh.CreateMethod('PdhCollectQueryData'); - GM.pdh.CreateMethod('PdhGetFormattedCounterValue'); - GM.pdh.CreateMethod('PdhGetFormattedCounterArrayA'); - GM.pdh.CreateMethod('PdhOpenQueryA'); - GM.pdh.CreateMethod('PdhRemoveCounter'); + GM.pdh.CreateMethod('PdhAddEnglishCounterA'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhaddenglishcountera + GM.pdh.CreateMethod('PdhCloseQuery'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhclosequery + GM.pdh.CreateMethod('PdhCollectQueryData'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhcollectquerydata + GM.pdh.CreateMethod('PdhGetFormattedCounterValue'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhgetformattedcountervalue + GM.pdh.CreateMethod('PdhGetFormattedCounterArrayA'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhgetformattedcounterarraya + GM.pdh.CreateMethod('PdhOpenQueryA'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhopenquerya + GM.pdh.CreateMethod('PdhRemoveCounter'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhremovecounter } +// +// Windows Implementation for fetching CPU utilization +// function windows_cpuUtilization() { var p = new promise(function (res, rej) { this._res = res; this._rej = rej; }); @@ -49,12 +58,14 @@ function windows_cpuUtilization() if ((err = GM.pdh.PdhCollectQueryData(p.cpu.Deref()).Val != 0)) { p._rej(err); return; } p._timeout = setTimeout(function (po) { + // Query in 100ms + var u = { cpus: [] }; var bufSize = GM.CreateVariable(4); var itemCount = GM.CreateVariable(4); var buffer, szName, item; var e; - if ((e = GM.pdh.PdhCollectQueryData(po.cpu.Deref()).Val != 0)) { po._rej(e); return; } + if ((e = GM.pdh.PdhCollectQueryData(po.cpu.Deref()).Val != 0)) { po._rej(e); return; /* Reject Promise if this fails */ } if ((e = GM.pdh.PdhGetFormattedCounterArrayA(po.cpuTotal.Deref(), PDH_FMT_DOUBLE, bufSize, itemCount, 0).Val) == -2147481646) { @@ -62,21 +73,22 @@ function windows_cpuUtilization() } else { + // Reject Promise if this failed po._rej(e); return; } - if ((e = GM.pdh.PdhGetFormattedCounterArrayA(po.cpuTotal.Deref(), PDH_FMT_DOUBLE, bufSize, itemCount, buffer).Val) != 0) { po._rej(e); return; } - for(var i=0;i