Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(detect-libc): faster musl check by looking for ldd file #19

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions benchmark/call-familySync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const performance = require('perf_hooks').performance;
const libc = require('..');

const now = performance.now();
libc.familySync();

console.log(`[family] Time Spent ${performance.now() - now}ms`);
7 changes: 7 additions & 0 deletions benchmark/call-isNonGlibcLinuxSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const performance = require('perf_hooks').performance;
const libc = require('..');

const now = performance.now();
libc.isNonGlibcLinuxSync();

console.log(`[isNonGlibcLinux] Time Spent ${performance.now() - now}ms`);
7 changes: 7 additions & 0 deletions benchmark/call-versionSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const performance = require('perf_hooks').performance;
const libc = require('..');

const now = performance.now();
libc.versionSync();

console.log(`[versionSync] Time Spent ${performance.now() - now}ms`);
39 changes: 39 additions & 0 deletions benchmark/detect-libc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const Benchmark = require('benchmark');
lovell marked this conversation as resolved.
Show resolved Hide resolved
const suite = new Benchmark.Suite();
const libc = require('../');

suite.add('family', async function () {
await libc.family();
});

suite.add('familySync', function () {
libc.familySync();
});

suite.add('version', async function () {
await libc.version();
});

suite.add('versionSync', function () {
libc.versionSync();
});

suite.add('isNonGlibcLinux', async function () {
await libc.isNonGlibcLinux();
});

suite.add('isNonGlibcLinuxSync', function () {
libc.isNonGlibcLinuxSync();
});

suite
// add listeners
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log('Fastest operation is ' + this.filter('fastest').map('name'));
})
.run({
async: true
});
106 changes: 102 additions & 4 deletions lib/detect-libc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

const childProcess = require('child_process');
const { isLinux, getReport } = require('./process');
const { LDD_PATH, readFile, readFileSync } = require('./filesystem');

let cachedFamilyFilesystem;
let cachedVersionFilesystem;

const command = 'getconf GNU_LIBC_VERSION 2>&1 || true; ldd --version 2>&1 || true';
let commandOut = '';
Expand Down Expand Up @@ -39,13 +43,31 @@ const safeCommandSync = () => {
*/
const GLIBC = 'glibc';

/**
* A Regexp constant to get the GLIBC Version.
* @type {string}
*/
const RE_GLIBC_VERSION = /GLIBC\s(\d+\.\d+)/;

/**
* A String constant containing the value `musl`.
* @type {string}
* @public
*/
const MUSL = 'musl';

/**
* This string is used to find if the {@link LDD_PATH} is GLIBC
* @type {string}
*/
const GLIBC_ON_LDD = GLIBC.toUpperCase();

/**
* This string is used to find if the {@link LDD_PATH} is musl
* @type {string}
*/
const MUSL_ON_LDD = MUSL.toLowerCase();
Copy link
Contributor

@styfle styfle Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This .toLowerCase() is redundant since MUSL on line 57 is already lowercase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this just to make sure the text is always lowercase and a change to musl won't affect this constant.
But better than removing .toLowerCase could be just putting the string literal, since they are different information even though it is the same string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, might be best to inline those strings in the getFamilyFromLddContent() function since each is only used once, no need for a variable

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PRs welcome 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created #20


const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-');

const familyFromReport = () => {
Expand All @@ -72,14 +94,51 @@ const familyFromCommand = (out) => {
return null;
};

const getFamilyFromLddContent = (content) => {
if (content.includes(MUSL_ON_LDD)) {
return MUSL;
}
if (content.includes(GLIBC_ON_LDD)) {
return GLIBC;
}
return null;
};

const familyFromFilesystem = async () => {
if (cachedFamilyFilesystem !== undefined) {
return cachedFamilyFilesystem;
}
cachedFamilyFilesystem = null;
try {
const lddContent = await readFile(LDD_PATH);
cachedFamilyFilesystem = getFamilyFromLddContent(lddContent);
} catch (e) {}
return cachedFamilyFilesystem;
};

const familyFromFilesystemSync = () => {
if (cachedFamilyFilesystem !== undefined) {
return cachedFamilyFilesystem;
}
cachedFamilyFilesystem = null;
try {
const lddContent = readFileSync(LDD_PATH);
cachedFamilyFilesystem = getFamilyFromLddContent(lddContent);
} catch (e) {}
return cachedFamilyFilesystem;
};

/**
* Resolves with the libc family when it can be determined, `null` otherwise.
* @returns {Promise<?string>}
*/
const family = async () => {
let family = null;
if (isLinux()) {
family = familyFromReport();
family = await familyFromFilesystem();
if (!family) {
family = familyFromReport();
}
if (!family) {
const out = await safeCommand();
family = familyFromCommand(out);
Expand All @@ -95,7 +154,10 @@ const family = async () => {
const familySync = () => {
let family = null;
if (isLinux()) {
family = familyFromReport();
family = familyFromFilesystemSync();
if (!family) {
family = familyFromReport();
}
if (!family) {
const out = safeCommandSync();
family = familyFromCommand(out);
Expand All @@ -116,6 +178,36 @@ const isNonGlibcLinux = async () => isLinux() && await family() !== GLIBC;
*/
const isNonGlibcLinuxSync = () => isLinux() && familySync() !== GLIBC;

const versionFromFilesystem = async () => {
if (cachedVersionFilesystem !== undefined) {
return cachedVersionFilesystem;
}
cachedVersionFilesystem = null;
try {
const lddContent = await readFile(LDD_PATH);
const versionMatch = lddContent.match(RE_GLIBC_VERSION);
if (versionMatch) {
cachedVersionFilesystem = versionMatch[1];
}
} catch (e) {}
return cachedVersionFilesystem;
};

const versionFromFilesystemSync = () => {
if (cachedVersionFilesystem !== undefined) {
return cachedVersionFilesystem;
}
cachedVersionFilesystem = null;
try {
const lddContent = readFileSync(LDD_PATH);
const versionMatch = lddContent.match(RE_GLIBC_VERSION);
if (versionMatch) {
cachedVersionFilesystem = versionMatch[1];
}
} catch (e) {}
return cachedVersionFilesystem;
};

const versionFromReport = () => {
const report = getReport();
if (report.header && report.header.glibcVersionRuntime) {
Expand Down Expand Up @@ -144,7 +236,10 @@ const versionFromCommand = (out) => {
const version = async () => {
let version = null;
if (isLinux()) {
version = versionFromReport();
version = await versionFromFilesystem();
if (!version) {
version = versionFromReport();
}
if (!version) {
const out = await safeCommand();
version = versionFromCommand(out);
Expand All @@ -160,7 +255,10 @@ const version = async () => {
const versionSync = () => {
let version = null;
if (isLinux()) {
version = versionFromReport();
version = versionFromFilesystemSync();
if (!version) {
version = versionFromReport();
}
if (!version) {
const out = safeCommandSync();
version = versionFromCommand(out);
Expand Down
36 changes: 36 additions & 0 deletions lib/filesystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require('fs');

/**
* The path where we can find the ldd
*/
const LDD_PATH = '/usr/bin/ldd';

/**
* Read the content of a file synchronous
*
* @param {string} path
* @returns {string}
*/
const readFileSync = path => fs.readFileSync(path, 'utf-8');

/**
* Read the content of a file
*
* @param {string} path
* @returns {Promise<string>}
*/
const readFile = path => new Promise((resolve, reject) => {
fs.readFile(path, 'utf-8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});

module.exports = {
LDD_PATH,
readFileSync,
readFile
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"index.d.ts"
],
"scripts": {
"test": "semistandard && nyc --reporter=lcov --check-coverage --branches=100 ava test/unit.js"
"test": "semistandard && nyc --reporter=lcov --check-coverage --branches=100 ava test/unit.js",
"bench": "node benchmark/detect-libc",
"bench:calls": "node benchmark/call-familySync.js && sleep 1 && node benchmark/call-isNonGlibcLinuxSync.js && sleep 1 && node benchmark/call-versionSync.js"
},
"repository": {
"type": "git",
Expand All @@ -26,6 +28,7 @@
"license": "Apache-2.0",
"devDependencies": {
"ava": "^2.4.0",
"benchmark": "^2.1.4",
"nyc": "^15.1.0",
"proxyquire": "^2.1.3",
"semistandard": "^14.2.3"
Expand Down
1 change: 1 addition & 0 deletions test/fixtexture-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1