How many threads can I run on my machine …?

Posted on

I was curious at one point how many threads I can run on my notebook in C++. To check you use the following:

/**
 * メイン
 */
int main()
{
        // ...
	unsigned int n = std::thread::hardware_concurrency();
	cout << n << " 本数のスレッドが実行可能です / No. of threads before you hit oversubscription" << endl;
        // ...
}

After checking on my machine..

➜  Benji:  clang++ -std=c++11 LOL_Machine.cpp
➜  Benji:  ./a.out
4 本数のスレッドが実行可能です / No. of threads before you hit oversubscription

So while searching for a command line how to check the statistics of my machine I came across an interesting thread from stack exchange. A guy called drfrogsplat had written a python script that checks all this for you. All you have to do is hit compile and done! Source code below:

# Author: drfrogsplat
# Link:   http://apple.stackexchange.com/questions/4286/is-there-a-mac-os-x-terminal-version-of-the-free-command-in-linux-systems

#!/usr/bin/python

import subprocess
import re

# Get process info
ps = subprocess.Popen(['ps', '-caxm', '-orss,comm'], stdout=subprocess.PIPE).communicate()[0]
vm = subprocess.Popen(['vm_stat'], stdout=subprocess.PIPE).communicate()[0]

# Iterate processes
processLines = ps.split('\n')
sep = re.compile('[\s]+')
rssTotal = 0 # kB
for row in range(1,len(processLines)):
    rowText = processLines[row].strip()
    rowElements = sep.split(rowText)
    try:
        rss = float(rowElements[0]) * 1024
    except:
        rss = 0 # ignore...
    rssTotal += rss

# Process vm_stat
vmLines = vm.split('\n')
sep = re.compile(':[\s]+')
vmStats = {}
for row in range(1,len(vmLines)-2):
    rowText = vmLines[row].strip()
    rowElements = sep.split(rowText)
    vmStats[(rowElements[0])] = int(rowElements[1].strip('\.')) * 4096

print 'Wired Memory:\t\t%d MB' % ( vmStats["Pages wired down"]/1024/1024 )
print 'Active Memory:\t\t%d MB' % ( vmStats["Pages active"]/1024/1024 )
print 'Inactive Memory:\t%d MB' % ( vmStats["Pages inactive"]/1024/1024 )
print 'Free Memory:\t\t%d MB' % ( vmStats["Pages free"]/1024/1024 )
print 'Real Mem Total (ps):\t%.3f MB' % ( rssTotal/1024/1024 )

Compile using python vm_stat.py..

Wired Memory:		1793 MB
Active Memory:		3313 MB
Inactive Memory:	728 MB
Free Memory:		1912 MB
Real Mem Total (ps):	5599.785 MB

Link: http://apple.stackexchange.com/questions/4286/is-there-a-mac-os-x-terminal-version-of-the-free-command-in-linux-systems

Useful stuff. Moving along to some more coding fun!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s