priority.cpp 1.93 KB
Newer Older
Vishesh Handa's avatar
Vishesh Handa committed
1
/* This file is part of the KDE Project
2
   SPDX-FileCopyrightText: 2008 Sebastian Trueg <trueg@kde.org>
Vishesh Handa's avatar
Vishesh Handa committed
3 4

   Parts of this file are based on code from Strigi
5
   SPDX-FileCopyrightText: 2006-2007 Jos van den Oever <jos@vandenoever.info>
Vishesh Handa's avatar
Vishesh Handa committed
6

7
   SPDX-License-Identifier: LGPL-2.0-or-later
Vishesh Handa's avatar
Vishesh Handa committed
8 9 10 11 12 13 14 15
*/

#include "priority.h"

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

Laurent Montel's avatar
Laurent Montel committed
16
#include <QDebug>
Vishesh Handa's avatar
Vishesh Handa committed
17 18

#ifndef _WIN32
Laurent Montel's avatar
Laurent Montel committed
19
#include <unistd.h>
Kevin Funk's avatar
Kevin Funk committed
20
#include <sys/resource.h>
Vishesh Handa's avatar
Vishesh Handa committed
21 22 23 24 25 26 27
#include <sys/syscall.h>
#include <errno.h>

#include <sched.h>
#endif

#ifdef SYS_ioprio_set
Laurent Montel's avatar
Laurent Montel committed
28
namespace {
Vishesh Handa's avatar
Vishesh Handa committed
29
#ifndef IOPRIO_CLASS_IDLE
Laurent Montel's avatar
Laurent Montel committed
30 31 32 33 34 35
enum {
    IOPRIO_CLASS_NONE,
    IOPRIO_CLASS_RT,
    IOPRIO_CLASS_BE,
    IOPRIO_CLASS_IDLE
};
Vishesh Handa's avatar
Vishesh Handa committed
36 37 38
#endif

#ifndef IOPRIO_WHO_PROCESS
Laurent Montel's avatar
Laurent Montel committed
39 40 41 42 43
enum {
    IOPRIO_WHO_PROCESS = 1,
    IOPRIO_WHO_PGRP,
    IOPRIO_WHO_USER
};
Vishesh Handa's avatar
Vishesh Handa committed
44 45 46
#endif

#ifndef IOPRIO_CLASS_SHIFT
Laurent Montel's avatar
Laurent Montel committed
47
const int IOPRIO_CLASS_SHIFT = 13;
Vishesh Handa's avatar
Vishesh Handa committed
48 49 50 51 52 53 54
#endif
}
#endif

bool lowerIOPriority()
{
#ifdef SYS_ioprio_set
Laurent Montel's avatar
Laurent Montel committed
55
    if (syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0, IOPRIO_CLASS_IDLE << IOPRIO_CLASS_SHIFT) < 0) {
Laurent Montel's avatar
Laurent Montel committed
56
        qDebug("cannot set io scheduling to idle (%s). Trying best effort.\n", strerror(errno));
Laurent Montel's avatar
Laurent Montel committed
57 58
        if (syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0, 7 | IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) < 0) {
            qDebug("cannot set io scheduling to best effort.\n");
Vishesh Handa's avatar
Vishesh Handa committed
59 60 61 62 63 64 65 66 67 68 69 70
            return false;
        }
    }
    return true;
#else
    return false;
#endif
}

bool lowerPriority()
{
#ifndef Q_OS_WIN
Laurent Montel's avatar
Laurent Montel committed
71
    return !setpriority(PRIO_PROCESS, 0, 19);
Vishesh Handa's avatar
Vishesh Handa committed
72 73 74 75 76 77 78 79 80
#else
    return false;
#endif
}

bool lowerSchedulingPriority()
{
#ifdef SCHED_BATCH
    struct sched_param param;
Laurent Montel's avatar
Laurent Montel committed
81
    memset(&param, 0, sizeof(param));
Vishesh Handa's avatar
Vishesh Handa committed
82
    param.sched_priority = 0;
Laurent Montel's avatar
Laurent Montel committed
83
    return !sched_setscheduler(0, SCHED_BATCH, &param);
Vishesh Handa's avatar
Vishesh Handa committed
84 85 86 87 88 89 90 91 92
#else
    return false;
#endif
}

bool setIdleSchedulingPriority()
{
#ifdef SCHED_IDLE
    struct sched_param param;
Laurent Montel's avatar
Laurent Montel committed
93
    memset(&param, 0, sizeof(param));
Vishesh Handa's avatar
Vishesh Handa committed
94
    param.sched_priority = 0;
Laurent Montel's avatar
Laurent Montel committed
95
    return !sched_setscheduler(0, SCHED_IDLE, &param);
Vishesh Handa's avatar
Vishesh Handa committed
96 97 98 99
#else
    return false;
#endif
}