]> git.ipfire.org Git - pakfire.git/commitdiff
Replace bogomips by fibonnacci benchmark
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 23 Nov 2016 22:36:52 +0000 (23:36 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 23 Nov 2016 22:36:52 +0000 (23:36 +0100)
BogoMIPS cannot be used as an easy way to determine how fast
a system is any more since there is only dummy values available
in newer kernels.

We use a simple fibonnacci function now which is executed for
a set amount of time (defaults to one second) and count the
number of iterations performed. This is then multiplied by the
number of CPU cores.

Fibonnacci is a very simple test that relies on the integer
unit (no FPU involved) and is not depending on anything else.

This will also allow us to quantitatively determine the
difference in speed between processors.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/_pakfiremodule.c
src/_pakfire/util.c
src/_pakfire/util.h
src/pakfire/system.py

index 04a321b6d9971918deefdd1da0c5960d3048cf02..fc2d953c6747089adff1125ee3b2204bfe9bea36 100644 (file)
@@ -40,6 +40,7 @@
 #include "util.h"
 
 static PyMethodDef pakfireModuleMethods[] = {
+       {"performance_index", (PyCFunction)performance_index, METH_VARARGS, NULL},
        {"version_compare", (PyCFunction)version_compare, METH_VARARGS, NULL},
        {"get_capabilities", (PyCFunction)get_capabilities, METH_VARARGS, NULL},
        {"set_capabilities", (PyCFunction)set_capabilities, METH_VARARGS, NULL},
index 616e0344e86795b912205357bf93a19159aed249..85fc72140be5c40c671f624e3c05ab337208c3a1 100644 (file)
@@ -23,6 +23,7 @@
 #include <errno.h>
 #include <sched.h>
 #include <sys/personality.h>
+#include <time.h>
 #include <unistd.h>
 
 #include "constants.h"
@@ -83,3 +84,55 @@ PyObject *version_compare(PyObject *self, PyObject *args) {
 
        return Py_BuildValue("i", ret);
 }
+
+static unsigned int fibonnacci(const clock_t* deadline) {
+       clock_t now = clock();
+
+       unsigned long f1 = 1;
+       unsigned long f2 = 1;
+
+       // Count iterations
+       unsigned int counter = 0;
+
+       while (now < *deadline) {
+               unsigned long next = f1 + f2;
+               f1 = f2;
+               f2 = next;
+
+               now = clock();
+               counter++;
+       }
+
+       return counter;
+}
+
+PyObject* performance_index(PyObject* self, PyObject* args) {
+       int seconds = 1;
+
+       if (!PyArg_ParseTuple(args, "|i", &seconds)) {
+               return NULL;
+       }
+
+       if (seconds == 0) {
+               PyErr_SetString(PyExc_ValueError, "Runtime must be one second or longer");
+               return NULL;
+       }
+
+       // Determine the number of online processors
+       int processors = sysconf(_SC_NPROCESSORS_ONLN);
+
+       // Determine deadline
+       clock_t deadline = clock();
+       deadline += CLOCKS_PER_SEC * seconds;
+
+       // Run Fibonnacci until deadline
+       unsigned int iterations = fibonnacci(&deadline);
+
+       // Times the result by the number of processors
+       iterations *= processors;
+
+       // Normalise to a second
+       iterations /= seconds;
+
+       return PyLong_FromUnsignedLong(iterations);
+}
index 0322d1c8f0cbbd3ce4f19591c8bd2ff210822c07..97d5bb3a4d0dabb21928b82de127c63eac1591a4 100644 (file)
@@ -29,5 +29,6 @@ extern PyObject *_personality(PyObject *self, PyObject *args);
 extern PyObject *_sync(PyObject *self, PyObject *args);
 extern PyObject *_unshare(PyObject *self, PyObject *args);
 extern PyObject *version_compare(PyObject *self, PyObject *args);
+extern PyObject* performance_index(PyObject* self, PyObject* args);
 
 #endif
index f6ff1dc57ea91742ceebb9d9754013346ee7b266..758a8d7ada57ab8aafbfa82024e5198238e7886e 100644 (file)
@@ -29,6 +29,8 @@ import tempfile
 import distro
 import shell
 
+from . import _pakfire
+
 from i18n import _
 
 class System(object):
@@ -151,16 +153,8 @@ class System(object):
                return ret or _("Could not be determined")
 
        @property
-       def cpu_bogomips(self):
-               cpuinfo = self.parse_cpuinfo()
-
-               for key in ("bogomips", "BogoMIPS"):
-                       bogomips = cpuinfo.get(key, None)
-
-                       if bogomips is None:
-                               continue
-
-                       return float(bogomips) * self.cpu_count
+        def cpu_bogomips(self):
+                return _pakfire.performance_index()
 
        def get_loadavg(self):
                return os.getloadavg()