If you write software where the customer pays for the number of CPU cores the software uses, then what would be the best way of achiving this in your C++ code? My research so far has led me to use SetProcessAffinityMask on Windows and sched_setaffinity on POSIX systems.
-
True and detailed answer for your question : [1]: stackoverflow.com/a/3082553/1848929hakki– hakki2013-02-26 00:50:28 +00:00Commented Feb 26, 2013 at 0:50
-
@hakiko That question is about finding out how many cores there are...us2012– us20122013-02-26 00:51:27 +00:00Commented Feb 26, 2013 at 0:51
-
2On Windows, you should have a look at job objects: msdn.microsoft.com/en-us/library/windows/desktop/… they can do a lot more for limiting processes than affinity. But they also can limit affinity: msdn.microsoft.com/en-us/library/windows/desktop/…Simon Mourier– Simon Mourier2013-03-08 10:53:01 +00:00Commented Mar 8, 2013 at 10:53
-
Is this your software or their software? How much control do you have over the machines? How exactly did you want to model your measurement metrics: If the software uses one core for most of the time but touches another core for 10 cycles are you going to count that as using two cores?Roasted Yam– Roasted Yam2013-03-10 14:33:02 +00:00Commented Mar 10, 2013 at 14:33
-
1"he customer pays for the number of CPU cores the software uses" - it's important how those cores are counted - if you don't run more thread than cores allowed, no more cores will be used any given time. Some OS-es will schedule those threads to all available cores from time to time, so it's up to computer owner how to count it. Mentioned functions should keep system from freely moving threads around, so that makes your question answered already.j_kubik– j_kubik2013-03-11 06:44:55 +00:00Commented Mar 11, 2013 at 6:44
2 Answers
That's an interesting question. I don't think I have the perfect solution, but since there has been no response so far, let me suggest the following:
If the main chunk of work in your program is done by one type of thread, just don't spawn more worker threads than the customer's license allows. As a single thread can't be divided to run on multiple cores, this imposes a hard limit.
(I don't think setting process CPU affinity is the way to go as it can be easily changed at runtime. Since it doesn't require any reverse engineering or permanent modifications to the system, I would be worried that circumventing this doesn't feel "bad" enough to prevent even the honest customers from trying it.)
1 Comment
GetProcessAffinityMask and throw a licensing exception(exit) if it has changed since I first set it in main.I think you found the best option. Limiting the number of threads is not a good idea if you want to take advantage of the capabilities of multithreaded processors.