| 1 | /* |
| 2 | This file is part of the KDE Project |
| 3 | SPDX-FileCopyrightText: 2008 Sebastian Trueg <trueg@kde.org> |
| 4 | |
| 5 | Parts of this file are based on code from Strigi |
| 6 | SPDX-FileCopyrightText: 2006-2007 Jos van den Oever <jos@vandenoever.info> |
| 7 | |
| 8 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 9 | */ |
| 10 | |
| 11 | #include "priority.h" |
| 12 | |
| 13 | #ifndef _GNU_SOURCE |
| 14 | #define _GNU_SOURCE |
| 15 | #endif |
| 16 | |
| 17 | #include <QDebug> |
| 18 | |
| 19 | #include <sys/time.h> |
| 20 | #include <sys/resource.h> |
| 21 | |
| 22 | #include <unistd.h> |
| 23 | #ifndef _WIN32 |
| 24 | #include <sys/syscall.h> |
| 25 | #include <cerrno> |
| 26 | |
| 27 | #include <sched.h> |
| 28 | #endif |
| 29 | |
| 30 | #ifdef SYS_ioprio_set |
| 31 | namespace { |
| 32 | #ifndef IOPRIO_CLASS_IDLE |
| 33 | enum { |
| 34 | IOPRIO_CLASS_NONE, |
| 35 | IOPRIO_CLASS_RT, |
| 36 | IOPRIO_CLASS_BE, |
| 37 | IOPRIO_CLASS_IDLE, |
| 38 | }; |
| 39 | #endif |
| 40 | |
| 41 | #ifndef IOPRIO_WHO_PROCESS |
| 42 | enum { |
| 43 | IOPRIO_WHO_PROCESS = 1, |
| 44 | IOPRIO_WHO_PGRP, |
| 45 | IOPRIO_WHO_USER, |
| 46 | }; |
| 47 | #endif |
| 48 | |
| 49 | #ifndef IOPRIO_CLASS_SHIFT |
| 50 | const int IOPRIO_CLASS_SHIFT = 13; |
| 51 | #endif |
| 52 | } |
| 53 | #endif |
| 54 | |
| 55 | bool lowerIOPriority() |
| 56 | { |
| 57 | #ifdef SYS_ioprio_set |
| 58 | if ( syscall( SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0, IOPRIO_CLASS_IDLE<<IOPRIO_CLASS_SHIFT ) < 0 ) { |
| 59 | qDebug( msg: "cannot set io scheduling to idle (%s). Trying best effort.\n" , strerror( errno )); |
| 60 | if ( syscall( SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0, 7|IOPRIO_CLASS_BE<<IOPRIO_CLASS_SHIFT ) < 0 ) { |
| 61 | qDebug( msg: "cannot set io scheduling to best effort.\n" ); |
| 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | return true; |
| 66 | #else |
| 67 | return false; |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | bool lowerPriority() |
| 72 | { |
| 73 | #ifndef Q_OS_WIN |
| 74 | return !setpriority( PRIO_PROCESS, who: 0, prio: 19 ); |
| 75 | #else |
| 76 | return false; |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | bool lowerSchedulingPriority() |
| 81 | { |
| 82 | #ifdef SCHED_BATCH |
| 83 | struct sched_param param; |
| 84 | memset( s: ¶m, c: 0, n: sizeof(param) ); |
| 85 | param.sched_priority = 0; |
| 86 | return !sched_setscheduler( pid: 0, SCHED_BATCH, param: ¶m ); |
| 87 | #else |
| 88 | return false; |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | bool setIdleSchedulingPriority() |
| 93 | { |
| 94 | #ifdef SCHED_IDLE |
| 95 | struct sched_param param; |
| 96 | memset( s: ¶m, c: 0, n: sizeof(param) ); |
| 97 | param.sched_priority = 0; |
| 98 | return !sched_setscheduler( pid: 0, SCHED_IDLE, param: ¶m ); |
| 99 | #else |
| 100 | return false; |
| 101 | #endif |
| 102 | } |
| 103 | |