]>
git.ipfire.org Git - people/ms/pakfire.git/blob - src/_pakfire/util.c
1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2011 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
25 #include <sys/personality.h>
29 #include "constants.h"
32 PyObject
*_personality(PyObject
*self
, PyObject
*args
) {
33 unsigned long persona
;
36 if (!PyArg_ParseTuple(args
, "l", &persona
)) {
37 /* XXX raise exception */
41 /* Change personality here. */
42 ret
= personality(persona
);
45 PyErr_SetString(PyExc_RuntimeError
, "Could not set personality.");
49 return Py_BuildValue("i", ret
);
52 PyObject
*_sync(PyObject
*self
, PyObject
*args
) {
53 /* Just sync everything to disks. */
59 PyObject
*_unshare(PyObject
*self
, PyObject
*args
) {
62 if (!PyArg_ParseTuple(args
, "i", &flags
)) {
66 int ret
= unshare(flags
);
68 return PyErr_SetFromErrno(PyExc_RuntimeError
);
71 return Py_BuildValue("i", ret
);
74 PyObject
*version_compare(PyObject
*self
, PyObject
*args
) {
76 const char *evr1
, *evr2
;
78 if (!PyArg_ParseTuple(args
, "Oss", &pool
, &evr1
, &evr2
)) {
79 /* XXX raise exception */
83 int ret
= pool_evrcmp_str(pool
, evr1
, evr2
, EVRCMP_COMPARE
);
85 return Py_BuildValue("i", ret
);
88 static unsigned long fibonnacci(const clock_t* deadline
) {
89 clock_t now
= clock();
95 unsigned long counter
= 0;
97 while (now
< *deadline
) {
98 unsigned long next
= f1
+ f2
;
109 PyObject
* performance_index(PyObject
* self
, PyObject
* args
) {
112 if (!PyArg_ParseTuple(args
, "|i", &seconds
)) {
117 PyErr_SetString(PyExc_ValueError
, "Runtime must be one second or longer");
121 // Determine the number of online processors
122 int processors
= sysconf(_SC_NPROCESSORS_ONLN
);
124 // Determine deadline
125 clock_t deadline
= clock();
126 deadline
+= CLOCKS_PER_SEC
* seconds
;
128 // Run Fibonnacci until deadline
129 unsigned long iterations
= fibonnacci(&deadline
);
131 // Times the result by the number of processors
132 iterations
*= processors
;
134 // Normalise to a second
135 iterations
/= seconds
;
137 return PyLong_FromUnsignedLong(iterations
);