-
Notifications
You must be signed in to change notification settings - Fork 16
perf(detect-libc): faster musl check by looking for ldd file #19
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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`); |
| 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`); |
| 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`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| const Benchmark = require('benchmark'); | ||
| 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 | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = ''; | ||
|
|
@@ -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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, might be best to inline those strings in the
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PRs welcome 😅
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = () => { | ||
|
|
@@ -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); | ||
|
|
@@ -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); | ||
|
|
@@ -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) { | ||
|
|
@@ -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); | ||
|
|
@@ -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); | ||
|
|
||
| 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 | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 1 |
Uh oh!
There was an error while loading. Please reload this page.