13

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.

5
  • True and detailed answer for your question : [1]: stackoverflow.com/a/3082553/1848929 Commented Feb 26, 2013 at 0:50
  • @hakiko That question is about finding out how many cores there are... Commented Feb 26, 2013 at 0:51
  • 2
    On 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/… Commented 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? Commented 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. Commented Mar 11, 2013 at 6:44

2 Answers 2

2

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

Setting thread quotas may also be a way of doing this. When it comes to people changing the process affinity at runtime I thought that I would occasionally check the affinity with GetProcessAffinityMask and throw a licensing exception(exit) if it has changed since I first set it in main.
0

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.

1 Comment

"where the customer pays for the number of CPU cores" already implies that you do NOT want to take advantage of those capabilities, UNLESS the customer pays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.