File indexing completed on 2024-04-21 03:51:46
0001 /* 0002 This file is part of the KDE Project 0003 SPDX-FileCopyrightText: 2008 Sebastian Trueg <trueg@kde.org> 0004 0005 Parts of this file are based on code from Strigi 0006 SPDX-FileCopyrightText: 2006-2007 Jos van den Oever <jos@vandenoever.info> 0007 0008 SPDX-License-Identifier: LGPL-2.0-or-later 0009 */ 0010 0011 #include "priority.h" 0012 0013 #ifndef _GNU_SOURCE 0014 #define _GNU_SOURCE 0015 #endif 0016 0017 #include <QDebug> 0018 0019 #include <sys/time.h> 0020 #include <sys/resource.h> 0021 0022 #include <unistd.h> 0023 #ifndef _WIN32 0024 #include <sys/syscall.h> 0025 #include <cerrno> 0026 0027 #include <sched.h> 0028 #endif 0029 0030 #ifdef SYS_ioprio_set 0031 namespace { 0032 #ifndef IOPRIO_CLASS_IDLE 0033 enum { 0034 IOPRIO_CLASS_NONE, 0035 IOPRIO_CLASS_RT, 0036 IOPRIO_CLASS_BE, 0037 IOPRIO_CLASS_IDLE, 0038 }; 0039 #endif 0040 0041 #ifndef IOPRIO_WHO_PROCESS 0042 enum { 0043 IOPRIO_WHO_PROCESS = 1, 0044 IOPRIO_WHO_PGRP, 0045 IOPRIO_WHO_USER, 0046 }; 0047 #endif 0048 0049 #ifndef IOPRIO_CLASS_SHIFT 0050 const int IOPRIO_CLASS_SHIFT = 13; 0051 #endif 0052 } 0053 #endif 0054 0055 bool lowerIOPriority() 0056 { 0057 #ifdef SYS_ioprio_set 0058 if ( syscall( SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0, IOPRIO_CLASS_IDLE<<IOPRIO_CLASS_SHIFT ) < 0 ) { 0059 qDebug( "cannot set io scheduling to idle (%s). Trying best effort.\n", strerror( errno )); 0060 if ( syscall( SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0, 7|IOPRIO_CLASS_BE<<IOPRIO_CLASS_SHIFT ) < 0 ) { 0061 qDebug( "cannot set io scheduling to best effort.\n"); 0062 return false; 0063 } 0064 } 0065 return true; 0066 #else 0067 return false; 0068 #endif 0069 } 0070 0071 bool lowerPriority() 0072 { 0073 #ifndef Q_OS_WIN 0074 return !setpriority( PRIO_PROCESS, 0, 19 ); 0075 #else 0076 return false; 0077 #endif 0078 } 0079 0080 bool lowerSchedulingPriority() 0081 { 0082 #ifdef SCHED_BATCH 0083 struct sched_param param; 0084 memset( ¶m, 0, sizeof(param) ); 0085 param.sched_priority = 0; 0086 return !sched_setscheduler( 0, SCHED_BATCH, ¶m ); 0087 #else 0088 return false; 0089 #endif 0090 } 0091 0092 bool setIdleSchedulingPriority() 0093 { 0094 #ifdef SCHED_IDLE 0095 struct sched_param param; 0096 memset( ¶m, 0, sizeof(param) ); 0097 param.sched_priority = 0; 0098 return !sched_setscheduler( 0, SCHED_IDLE, ¶m ); 0099 #else 0100 return false; 0101 #endif 0102 }