OpenCores
URL https://opencores.org/ocsvn/w11/w11/trunk

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.74/] [tools/] [bin/] [njobihtm] - Blame information for rev 38

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 37 wfjm
#!/usr/bin/perl -w
2
# $Id: njobihtm 810 2016-10-02 16:51:12Z mueller $
3
#
4
# Copyright 2016- by Walter F.J. Mueller 
5
#
6
# This program is free software; you may redistribute and/or modify it under
7
# the terms of the GNU General Public License as published by the Free
8
# Software Foundation, either version 2, or at your option any later version.
9
#
10
# This program is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
12
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13
# for complete details.
14
#
15
#  Revision History:
16
# Date         Rev Version  Comment
17
# 2016-10-01   810   1.0    Initial version
18
#
19
 
20
use 5.14.0;                                 # require Perl 5.14 or higher
21
use strict;                                 # require strict checking
22
 
23
use Getopt::Long;
24
 
25
my %opts = ();
26
 
27
GetOptions(\%opts, "verbose", "mem=s"
28
          )
29
  or die "bad options";
30
 
31
sub get_cpuinfo;
32
sub get_meminfo;
33
 
34
my $ncpu;
35
my $ntpc;
36
my $nkb;
37
my $njob = 1;
38
 
39
get_cpuinfo();
40
get_meminfo();
41
 
42
if (defined $ncpu && defined $ntpc && defined $nkb) {
43
} else {
44
  print STDERR "njobihtm-F: failed to obtain cpu or mem size\n";
45
  exit 1;
46
}
47
 
48
 
49
my $ncore = $ncpu / $ntpc;                  # number of cores
50
my $nht   = $ncpu - $ncore;
51
 
52
$njob = $ncore + int($nht/4);
53
 
54
if ($opts{verbose}) {
55
  printf STDERR "#cpus:        %d\n", $ncpu;
56
  printf STDERR "#thread/cpu:  %d\n", $ntpc;
57
  printf STDERR "#cores:       %d\n", $ncore;
58
  printf STDERR "mem(MB):      %d\n", int($nkb/1024);
59
  printf STDERR "#job (cpus):  %d\n", $njob;
60
}
61
 
62
if (defined $opts{mem}) {
63
  my $mem;
64
  if ($opts{mem} =~ m/^(\d+)([MG])$/) {
65
    $mem =       1024 * $1 if $2 eq 'M';
66
    $mem = 1024* 1024 * $1 if $2 eq 'G';
67
    my $njobm = int(($nkb - 1024*1024) / $mem);
68
    $njobm = 1 unless $njobm > 0;
69
    printf STDERR "#job (mem):   %d\n", $njobm if $opts{verbose};
70
    if ($njobm < $njob) {
71
      $njob = $njobm;
72
    }
73
  } else {
74
    print STDERR "njobihtm-F: bad -mem option '$opts{mem}', must be nnn[MG]\n";
75
    exit 1;
76
  }
77
}
78
 
79
print "$njob\n";
80
 
81
exit 0;
82
 
83
#-------------------------------------------------------------------------------
84
sub get_cpuinfo {
85
  open (LSCPU, "lscpu|")
86
    or die "failed to open 'lscpu|': $!";
87
 
88
  while () {
89
    chomp;
90
    if (m/^(.*?)\s*:\s*(.*)$/) {
91
      my $tag = $1;
92
      my $val = $2;
93
      # print "+++1 '$tag' : '$val' \n";
94
      $ncpu = $val if $tag eq 'CPU(s)';
95
      $ntpc = $val if $tag eq 'Thread(s) per core';
96
    }
97
  }
98
  close LSCPU;
99
  return;
100
}
101
 
102
#-------------------------------------------------------------------------------
103
sub get_meminfo {
104
  open (MEMINFO, "/proc/meminfo")
105
    or die "failed to open '/proc/meminfo': $!";
106
 
107
  while () {
108
    chomp;
109
    if (m/^(.*?)\s*:\s*(\d+)\s*kB/) {
110
      my $tag = $1;
111
      my $val = $2;
112
      # print "+++1 '$tag' : '$val' \n";
113
      $nkb = $val if $tag eq 'MemTotal';
114
    }
115
  }
116
  close MEMINFO;
117
  return;
118
}
119
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.