#!/usr/bin/perl -w # $Id: njobihtm 1244 2022-06-03 14:06:30Z mueller $ # SPDX-License-Identifier: GPL-3.0-or-later # Copyright 2016-2022 by Walter F.J. Mueller # # Revision History: # Date Rev Version Comment # 2022-06-03 1244 1.1 add -n and -h options # 2018-12-18 1089 1.0.1 add and use bailout # 2016-10-01 810 1.0 Initial version # use 5.14.0; # require Perl 5.14 or higher use strict; # require strict checking use Getopt::Long; my %opts = (); GetOptions(\%opts, "nohyper", "mem=s", "verbose", "help" ) or bailout("bad command options"); my $ncpu; my $ntpc; my $nkb; my $njob = 1; if ($opts{help}) { print_help(); exit 0; } get_cpuinfo(); get_meminfo(); bailout("failed to obtain cpu or mem size") unless (defined $ncpu && defined $ntpc && defined $nkb); my $ncore = $ncpu / $ntpc; # number of cores my $nht = $ncpu - $ncore; $njob = $ncore; $njob += int($nht/4) unless (defined $opts{nohyper}); if ($opts{verbose}) { printf STDERR "#cpus: %d\n", $ncpu; printf STDERR "#thread/cpu: %d\n", $ntpc; printf STDERR "#cores: %d\n", $ncore; printf STDERR "#mem(MB): %d\n", int($nkb/1024); printf STDERR "#job (cpus): %d\n", $njob; } if (defined $opts{mem}) { my $mem; if ($opts{mem} =~ m/^(\d+)([MG])$/) { $mem = 1024 * $1 if $2 eq 'M'; $mem = 1024* 1024 * $1 if $2 eq 'G'; my $njobm = int(($nkb - 1024*1024) / $mem); $njobm = 1 unless $njobm > 0; printf STDERR "#job (mem): %d\n", $njobm if $opts{verbose}; if ($njobm < $njob) { $njob = $njobm; } } else { bailout("bad -mem option '$opts{mem}', must be nnn[MG]"); } } print "$njob\n"; exit 0; #------------------------------------------------------------------------------- sub get_cpuinfo { open (LSCPU, "lscpu|") or bailout("failed to open 'lscpu|': $!"); while () { chomp; if (m/^(.*?)\s*:\s*(.*)$/) { my $tag = $1; my $val = $2; # print "+++1 '$tag' : '$val' \n"; $ncpu = $val if $tag eq 'CPU(s)'; $ntpc = $val if $tag eq 'Thread(s) per core'; } } close LSCPU; return; } #------------------------------------------------------------------------------- sub get_meminfo { open (MEMINFO, "/proc/meminfo") or bailout("failed to open '/proc/meminfo': $!"); while () { chomp; if (m/^(.*?)\s*:\s*(\d+)\s*kB/) { my $tag = $1; my $val = $2; # print "+++1 '$tag' : '$val' \n"; $nkb = $val if $tag eq 'MemTotal'; } } close MEMINFO; return; } #------------------------------------------------------------------------------- sub bailout { my ($msg) = @_; print STDERR "njobihtm-F: $msg\n"; exit 1; } #------------------------------------------------------------------------------- sub print_help { print "usage: njobihtm [options]\n"; print " Options:\n"; print " -n ignore hyperthreads\n"; print " -m nn[MG] memory per job as integer followed M or G\n"; print " -v print system parameters to STDERR\n"; print " -h this message\n"; }