-
Notifications
You must be signed in to change notification settings - Fork 7
/
spm_platform.m
269 lines (227 loc) · 9.57 KB
/
spm_platform.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
function varargout = spm_platform(varargin)
% Platform specific configuration parameters
%
% FORMAT ans = spm_platform(param)
% param - optional string argument, can be
% - 'bigend' - return whether this architecture is big endian
% - false - is little endian
% - true - is big endian
% - 'user' - return username
% - 'host' - return system's host name
% - 'tempdir' - return name of temp directory
% - 'desktop' - return whether or not the Desktop is in use
%
% FORMAT PlatFontNames = spm_platform('fonts')
% Return structure with fields named after the generic (UNIX) fonts, the
% field containing the name of the platform specific font.
%
% FORMAT PlatFontName = spm_platform('font',GenFontName)
% Map generic (UNIX) FontNames to platform specific FontNames
%
% FORMAT meminfo = spm_platform('memory',['available','total'])
% Return memory information concerning the amount of available physical
% memory or the total amount of physical memory.
%
% FORMAT PLATFORM = spm_platform
% Initialise platform specific parameters in persistent variable.
% PLATFORM - copy of persistent variable containing platform specific
% parameters.
%
% FORMAT PLATFORM = spm_platform('init')
% (Re)initialise platform specific parameters in persistent variable.
%
%--------------------------------------------------------------------------
% Since calls to spm_platform will be made frequently, most platform
% specific parameters are stored in a persistent variable.
% Subsequent calls use the information from this persistent variable, if
% it exists.
%__________________________________________________________________________
% Copyright (C) 1999-2020 Wellcome Centre for Human Neuroimaging
% Matthew Brett
% $Id: spm_platform.m 7861 2020-05-21 11:39:27Z guillaume $
%-Initialise
%--------------------------------------------------------------------------
persistent PLATFORM
if isempty(PLATFORM), PLATFORM = init_platform; end
if ~nargin, varargout = {PLATFORM}; return, end
switch lower(varargin{1}), case 'init' %-(re)initialise
%==========================================================================
PLATFORM = init_platform;
varargout = {PLATFORM};
case 'bigend' %-Return endianness for this architecture
%==========================================================================
varargout = {PLATFORM.bigend};
case 'filesys' %-Return file system (deprecated)
%==========================================================================
varargout = {PLATFORM.filesys};
case 'user' %-Return user name
%==========================================================================
varargout = {PLATFORM.user};
case 'host' %-Return host name
%==========================================================================
varargout = {PLATFORM.host};
case 'tempdir' %-Return temporary directory
%==========================================================================
twd = getenv('SPMTMP');
if isempty(twd)
twd = tempdir;
end
varargout = {twd};
case {'font','fonts'} %-Map default font names to platform font names
%==========================================================================
if nargin<2, varargout={PLATFORM.font}; return, end
switch lower(varargin{2})
case 'times'
varargout = {PLATFORM.font.times};
case 'courier'
varargout = {PLATFORM.font.courier};
case 'helvetica'
varargout = {PLATFORM.font.helvetica};
case 'symbol'
varargout = {PLATFORM.font.symbol};
otherwise
warning(['Unknown font ',varargin{2},', using default'])
varargout = {PLATFORM.font.helvetica};
end
case 'desktop' %-Return desktop usage
%==========================================================================
varargout = {PLATFORM.desktop};
case 'memory' %-Return memory information
%==========================================================================
varargout = {meminfo(varargin{2:end})};
otherwise %-Unknown Action string
%==========================================================================
error('Unknown Action string')
%==========================================================================
end
%==========================================================================
%- S U B - F U N C T I O N S
%==========================================================================
function PLATFORM = init_platform %-Initialise platform variables
%==========================================================================
if strcmpi(spm_check_version,'matlab')
comp = computer;
else
if isunix
comp = uname.machine;
switch comp
case {'x86_64'}
comp = 'GLNXA64';
case {'armv6l','armv7l','armv8l','aarch64'}
comp = 'ARM';
otherwise
error('%s is not supported.',comp);
end
elseif ispc
comp = 'PCWIN64';
elseif ismac
comp = 'MACI64';
end
end
%-Platform definitions
%--------------------------------------------------------------------------
PDefs = {'PCWIN64', 'win', false;...
'MACI64', 'unx', false;...
'GLNXA64', 'unx', false;...
'ARM', 'unx', false};
PDefs = cell2struct(PDefs,{'computer','filesys','endian'},2);
%-Which computer?
%--------------------------------------------------------------------------
[issup, ci] = ismember(comp,{PDefs.computer});
if ~issup
error([comp ' not supported architecture for ' spm('Ver')]);
end
%-Set byte ordering
%--------------------------------------------------------------------------
PLATFORM.bigend = PDefs(ci).endian;
%-Set filesystem type
%--------------------------------------------------------------------------
PLATFORM.filesys = PDefs(ci).filesys;
%-File separator character
%--------------------------------------------------------------------------
PLATFORM.sepchar = filesep;
%-Username
%--------------------------------------------------------------------------
switch PLATFORM.filesys
case 'unx'
PLATFORM.user = getenv('USER');
case 'win'
PLATFORM.user = getenv('USERNAME');
end
if isempty(PLATFORM.user), PLATFORM.user = 'anonymous'; end
%-Hostname
%--------------------------------------------------------------------------
switch PLATFORM.filesys
case 'unx'
[sts, PLATFORM.host] = system('hostname');
if sts
PLATFORM.host = getenv('HOSTNAME');
else
PLATFORM.host = PLATFORM.host(1:end-1);
end
case 'win'
PLATFORM.host = getenv('COMPUTERNAME');
end
PLATFORM.host = strtok(PLATFORM.host,'.');
%-Fonts
%--------------------------------------------------------------------------
switch comp
case 'MACI64'
PLATFORM.font.helvetica = 'TrebuchetMS';
PLATFORM.font.times = 'Times';
PLATFORM.font.courier = 'Courier';
PLATFORM.font.symbol = 'Symbol';
case {'GLNXA64','ARM'}
PLATFORM.font.helvetica = 'Helvetica';
PLATFORM.font.times = 'Times';
PLATFORM.font.courier = 'Courier';
PLATFORM.font.symbol = 'Symbol';
case 'PCWIN64'
PLATFORM.font.helvetica = 'Arial Narrow';
PLATFORM.font.times = 'Times New Roman';
PLATFORM.font.courier = 'Courier New';
PLATFORM.font.symbol = 'Symbol';
end
%-Desktop
%--------------------------------------------------------------------------
try
PLATFORM.desktop = usejava('desktop');
catch
PLATFORM.desktop = false;
end
function mem = meminfo(opt) %-Memory information
%==========================================================================
try
if ispc
% https://www.mathworks.com/help/matlab/ref/memory.html
[uv,sv] = memory;
mem.avail = sv.PhysicalMemory.Available; % or uv.MemAvailableAllArrays
mem.total = sv.PhysicalMemory.Total;
elseif ismac
% https://www.unix.com/man-page/osx/1/vm_stat/
[sts,m] = system('vm_stat'); % (page size of 4096 bytes)
m = strsplit(m,{':',sprintf('\n')});
mem.avail = str2double(m{find(ismember(m,'Pages free'))+1}) * 4096;
mem.avail = mem.avail + str2double(m{find(ismember(m,'Pages inactive'))+1}) * 4096;
mem.total = mem.avail + str2double(m{find(ismember(m,'Pages active'))+1}) * 4096;
mem.total = mem.total + str2double(m{find(ismember(m,'Pages speculative'))+1}) * 4096;
mem.total = mem.total + str2double(m{find(ismember(m,'Pages wired down'))+1}) * 4096;
mem.total = mem.total + str2double(m{find(ismember(m,'Pages occupied by compressor'))+1}) * 4096;
else
% http://man7.org/linux/man-pages/man5/proc.5.html
m = strsplit(fileread('/proc/meminfo')); % (in kB)
mem.avail = str2double(m{find(ismember(m,'MemAvailable:'))+1}) * 1024;
mem.total = str2double(m{find(ismember(m,'MemTotal:'))+1}) * 1024;
end
catch
mem = struct('avail',NaN,'total',NaN);
end
if ~nargin, return, end
switch lower(opt)
case 'total'
mem = mem.total;
case 'available'
mem = mem.avail;
otherwise
error('Unknown memory option.');
end