summaryrefslogtreecommitdiff
path: root/zen/process_priority.cpp
blob: 96a6c2de2b72ba7ef0e6dcd262bea01034f2bd81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved        *
// **************************************************************************

#include "process_priority.h"
#include <zen/last_error.h>
#include <zen/i18n.h>

#ifdef FFS_WIN
#include "win.h" //includes "windows.h"

#elif defined FFS_LINUX
//#include <sys/syscall.h>

#elif defined FFS_MAC
#include <sys/resource.h> //getiopolicy_np
#include <IOKit/pwr_mgt/IOPMLib.h> //keep in .cpp file to not pollute global namespace! e.g. with UInt64
#endif

using namespace zen;


#ifdef FFS_WIN
struct PreventStandby::Pimpl {};

PreventStandby::PreventStandby()
{
    if (::SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED) == 0)
        throw FileError(_("Failed to suspend system sleep mode.")); //no GetLastError() support?
}


PreventStandby::~PreventStandby()
{
    ::SetThreadExecutionState(ES_CONTINUOUS);
}


#ifndef PROCESS_MODE_BACKGROUND_BEGIN
#define PROCESS_MODE_BACKGROUND_BEGIN     0x00100000 // Windows Server 2003 and Windows XP/2000:  This value is not supported!
#define PROCESS_MODE_BACKGROUND_END       0x00200000 //
#endif

struct ScheduleForBackgroundProcessing::Pimpl {};


ScheduleForBackgroundProcessing::ScheduleForBackgroundProcessing()
{
    if (!::SetPriorityClass(::GetCurrentProcess(), PROCESS_MODE_BACKGROUND_BEGIN)) //this call lowers CPU priority, too!!
        throw FileError(_("Cannot change process I/O priorities.") + L"\n\n" + getLastErrorFormatted());
}


ScheduleForBackgroundProcessing::~ScheduleForBackgroundProcessing()
{
    ::SetPriorityClass(::GetCurrentProcess(), PROCESS_MODE_BACKGROUND_END);
}

#elif defined FFS_LINUX
struct PreventStandby::Pimpl {};
PreventStandby::PreventStandby() {}
PreventStandby::~PreventStandby() {}

//solution for GNOME?: http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html#org.gnome.SessionManager.Inhibit

struct ScheduleForBackgroundProcessing::Pimpl {};
ScheduleForBackgroundProcessing::ScheduleForBackgroundProcessing() {};
ScheduleForBackgroundProcessing::~ScheduleForBackgroundProcessing() {};

/*
struct ScheduleForBackgroundProcessing
{
	- required functions ioprio_get/ioprio_set are not part of glibc: http://linux.die.net/man/2/ioprio_set
	- and probably never will: http://sourceware.org/bugzilla/show_bug.cgi?id=4464
	- /usr/include/linux/ioprio.h not available on Ubuntu, so we can't use it instead

	ScheduleForBackgroundProcessing() : oldIoPrio(getIoPriority(IOPRIO_WHO_PROCESS, ::getpid()))
	{
		if (oldIoPrio != -1)
			setIoPriority(::getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
	}
	~ScheduleForBackgroundProcessing()
	{
		if (oldIoPrio != -1)
			setIoPriority(::getpid(), oldIoPrio);
	}

private:
	static int getIoPriority(pid_t pid)
	{
		return ::syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, pid);
	}
	static int setIoPriority(pid_t pid, int ioprio)
	{
		return ::syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, pid, ioprio);
	}

	const int oldIoPrio;
};
*/

#elif defined FFS_MAC
//https://developer.apple.com/library/mac/#qa/qa1340
struct PreventStandby::Pimpl
{
    Pimpl() : assertionID() {}
    IOPMAssertionID assertionID;
};

PreventStandby::PreventStandby() : pimpl(make_unique<Pimpl>())
{
    if (::IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep,
                                      kIOPMAssertionLevelOn,
                                      CFSTR("FreeFileSync"),
                                      &pimpl->assertionID) != kIOReturnSuccess)
        throw FileError(_("Failed to suspend system sleep mode."));
}

PreventStandby::~PreventStandby()
{
    ::IOPMAssertionRelease(pimpl->assertionID);
}


struct ScheduleForBackgroundProcessing::Pimpl
{
    Pimpl() : oldIoPrio() {}
    int oldIoPrio;
};


ScheduleForBackgroundProcessing::ScheduleForBackgroundProcessing() : pimpl(make_unique<Pimpl>())
{
    pimpl->oldIoPrio = ::getiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_PROCESS);
    if (pimpl->oldIoPrio == -1)
        throw FileError(_("Cannot change process I/O priorities.") + L" (r)" + L"\n\n" + getLastErrorFormatted());

    if (::setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_PROCESS, IOPOL_THROTTLE) != 0)
        throw FileError(_("Cannot change process I/O priorities.") + L" (w)" + L"\n\n" + getLastErrorFormatted());
}


ScheduleForBackgroundProcessing::~ScheduleForBackgroundProcessing()
{
    ::setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_PROCESS, pimpl->oldIoPrio);
}
#endif
bgstack15