Skip to content

Instantly share code, notes, and snippets.

@jaraco
Created March 8, 2019 15:46
Show Gist options
  • Save jaraco/5502059bb64531b115944d6eba388eef to your computer and use it in GitHub Desktop.
Save jaraco/5502059bb64531b115944d6eba388eef to your computer and use it in GitHub Desktop.
sysctl $ cat Dockerfile
from ubuntu:bionic
RUN apt update > /dev/null 2>&1
RUN apt install -y g++ vim > /dev/null 2>&1
COPY machineInfo.cpp .
RUN echo | g++ -dM -E - | grep linux
RUN g++ machineInfo.cpp
sysctl $ cat machineInfo.cpp
#include <sys/sysctl.h>
#include <linux/sysctl.h>
#include <cstdio>
#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
int num_procs(void) {
int numCPU = 0;
int nprocs;
size_t len = sizeof(nprocs);
static int mib[2] = { CTL_HW, HW_NCPU };
/* get the number of CPUs from the system */
sysctl(mib, 2, &numCPU, &len, NULL, 0);
if( numCPU < 1 )
{
mib[1] = HW_NCPU;
if (sysctl (mib, ARRAY_SIZE(mib), &nprocs, &len, NULL, 0) == 0 && len == sizeof (nprocs) && 0 < nprocs)
numCPU = nprocs;
if( numCPU < 1 )
numCPU = 1;
}
return numCPU;
}
sysctl $ docker build . --no-cache
Sending build context to Docker daemon 3.584kB
Step 1/6 : from ubuntu:bionic
---> 47b19964fb50
Step 2/6 : RUN apt update > /dev/null 2>&1
---> Running in d28dfd3539fc
Removing intermediate container d28dfd3539fc
---> 49cb6d48c991
Step 3/6 : RUN apt install -y g++ vim > /dev/null 2>&1
---> Running in beb45f9a69b7
Removing intermediate container beb45f9a69b7
---> 1d6014480000
Step 4/6 : COPY machineInfo.cpp .
---> 8dc7bc82b7f8
Step 5/6 : RUN echo | g++ -dM -E - | grep linux
---> Running in 6b391c4ee975
#define __linux 1
#define __linux__ 1
#define __gnu_linux__ 1
#define linux 1
Removing intermediate container 6b391c4ee975
---> e1a2afd74092
Step 6/6 : RUN g++ machineInfo.cpp
---> Running in 34016ec497a3
machineInfo.cpp: In function 'int num_procs()':
machineInfo.cpp:12:27: error: 'CTL_HW' was not declared in this scope
static int mib[2] = { CTL_HW, HW_NCPU };
^~~~~~
machineInfo.cpp:12:27: note: suggested alternative: 'CTL_PM'
static int mib[2] = { CTL_HW, HW_NCPU };
^~~~~~
CTL_PM
machineInfo.cpp:12:35: error: 'HW_NCPU' was not declared in this scope
static int mib[2] = { CTL_HW, HW_NCPU };
^~~~~~~
The command '/bin/sh -c g++ machineInfo.cpp' returned a non-zero code: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment