Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

""" 

Information about available RAM/swap 

 

There is no portable way to figure these out, nor should you generally 

have to. But GAP currently needs to allocate a cache of fixed size 

upon startup, and we would like a certain fraction of the swap address 

space. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo, MemoryInfo_proc 

doctest:... 

DeprecationWarning: the module sage.misc.memory_info is deprecated, use the psutil package instead. 

See http://trac.sagemath.org/21805 for details. 

sage: mem = MemoryInfo() 

sage: mem.total_ram() # random output 

16708194304 

sage: mem.available_ram() # random output 

1690738688 

sage: mem.total_swap() # random output 

15728635904 

sage: mem.available_swap() # random output 

15340593152 

""" 

 

#***************************************************************************** 

# Copyright (C) 2012 Volker Braun <vbraun.name@gmail.com> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

from sage.misc.superseded import deprecation 

deprecation(21805, "the module sage.misc.memory_info is deprecated, use the psutil package instead.") 

 

 

import subprocess 

from sys import maxsize 

from sage.structure.sage_object import SageObject 

 

memory_info_instance = None 

 

def MemoryInfo(): 

""" 

Provide information about memory 

 

OUTPUT: 

 

A class that is encapsulates memory information. If no method for 

the particular host OS is provided, reasonable guesses are given. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo, MemoryInfo_proc 

sage: mem = MemoryInfo() 

sage: mem.total_ram() # random output 

16708194304 

sage: mem.available_ram() # random output 

1690738688 

sage: mem.total_swap() # random output 

15728635904 

sage: mem.available_swap() # random output 

15340593152 

""" 

global memory_info_instance 

if memory_info_instance is not None: 

return memory_info_instance 

import platform 

system = platform.system() 

if memory_info_instance is None and \ 

system != 'Darwin': 

try: 

memory_info_instance = MemoryInfo_proc() 

except OSError: 

pass 

if memory_info_instance is None and \ 

system == 'Darwin': 

try: 

memory_info_instance = MemoryInfo_OSX() 

except OSError: 

pass 

if memory_info_instance is None: 

memory_info_instance = MemoryInfo_guess() 

return memory_info_instance 

 

 

class MemoryInfo_base(SageObject): 

""" 

Base class for memory info objects. 

""" 

 

def rlimit_address_space(self): 

""" 

Return ``RLIMIT_AS``. 

 

OUTPUT: 

 

Integer. The limit in bytes or `-1` if no limit is set or cannot 

be found out. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: mem = MemoryInfo() 

sage: mem.rlimit_address_space() in ZZ 

True 

""" 

import resource 

try: 

limit = resource.getrlimit(resource.RLIMIT_AS)[0] 

except resource.error: 

return -1 

if limit == resource.RLIM_INFINITY: 

return -1 

return limit 

 

def virtual_memory_limit(self): 

""" 

Return the upper limit for virtual memory usage 

 

This is the value set by ``ulimit -v`` at the command line 

(bounded by ``sys.maxsize``) or a practical limit if no limit 

is set. 

 

OUTPUT: 

 

Integer. The virtual memory limit in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: mem = MemoryInfo() 

sage: mem.virtual_memory_limit() > 0 

True 

sage: mem.virtual_memory_limit() <= sys.maxsize 

True 

""" 

limit = self.rlimit_address_space() 

if limit < 0: 

limit = self.total_swap() + self.total_ram() 

 

# Use less than half of the addressable memory 

return min(maxsize, limit) 

 

 

class MemoryInfo_proc(MemoryInfo_base): 

""" 

Provide information from ``/proc/`` pseudo-filesystem on most UNIXes 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: mem = MemoryInfo() 

sage: mem.total_ram() # random output 

16708194304 

""" 

 

def __init__(self): 

try: 

self._parse_proc_meminfo() 

except (IOError, ValueError): 

raise OSError('/proc/meminfo is not available') 

 

def _parse_proc_meminfo(self): 

""" 

Parse ``/proc/meminfo`` 

 

OUTPUT: 

 

A dictionary. All sizes are in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo, MemoryInfo_proc 

sage: mem = MemoryInfo() 

sage: if isinstance(mem, MemoryInfo_proc): 

....: info = mem._parse_proc_meminfo() 

....: else: 

....: info = None 

sage: info # random output 

{'available_ram': 1749782528, 

'total_swap': 15728635904, 

'free_swap': 15340572672, 

'total_ram': 16708194304} 

sage: keys = set(['available_ram', 'total_swap', 'free_swap', 'total_ram']) 

sage: (info is None) or keys.issubset(info.keys()) 

True 

""" 

kb = 1024 

result = dict() 

meminfo = open('/proc/meminfo', 'r') 

for line in meminfo.readlines(): 

line = line.split() 

if line[0].startswith('MemTotal') and line[2] == 'kB': 

result['total_ram'] = int(line[1]) * kb 

if line[0].startswith('MemFree') and line[2] == 'kB': 

result['available_ram'] = int(line[1]) * kb 

if line[0].startswith('SwapTotal') and line[2] == 'kB': 

result['total_swap'] = int(line[1]) * kb 

if line[0].startswith('SwapFree') and line[2] == 'kB': 

result['free_swap'] = int(line[1]) * kb 

if line[0].startswith('Committed_AS') and line[2] == 'kB': 

result['Committed_AS'] = int(line[1]) * kb 

meminfo.close() 

required = set(['available_ram', 'total_swap', 'free_swap', 'total_ram']) 

if not required.issubset(result.keys()): 

raise OSError('failed to parse /proc/meminfo correctly') 

return result 

 

def total_ram(self): 

""" 

Return the total RAM size 

 

OUTPUT: 

 

Integer. The RAM size in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: MemoryInfo().total_ram() > 0 

True 

""" 

return self._parse_proc_meminfo()['total_ram'] 

 

def available_ram(self): 

""" 

Return the available (free) RAM size 

 

OUTPUT: 

 

Integer. The free RAM size in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: MemoryInfo().available_ram() > 0 

True 

""" 

return self._parse_proc_meminfo()['available_ram'] 

 

def total_swap(self): 

""" 

Return the total swap size 

 

OUTPUT: 

 

Integer. The swap size in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: MemoryInfo().total_swap() >= 0 

True 

""" 

return self._parse_proc_meminfo()['total_swap'] 

 

def available_swap(self): 

""" 

Return the available (free) swap size 

 

OUTPUT: 

 

Integer. The free swap size in bytes, excluding reserved swap 

space. Can be negative if the system is overcommitting memory. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: MemoryInfo().available_swap() in ZZ 

True 

""" 

info = self._parse_proc_meminfo() 

try: 

return info['total_swap'] - info['Committed_AS'] 

except KeyError: 

return info['free_swap'] 

 

 

class MemoryInfo_OSX(MemoryInfo_base): 

""" 

Memory info on OSX 

 

TESTS:: 

 

sage: from sage.misc.memory_info import MemoryInfo_OSX 

""" 

 

def __init__(self): 

self._maxage = 10 # cache result for 10 seconds 

self._age = -self._maxage 

try: 

self._parse_top() 

except (IOError, ValueError, subprocess.CalledProcessError, KeyError): 

raise OSError('failed to parse OSX "top" output') 

 

def _parse_top_output(self, meminfo): 

""" 

Pick total and available memory out of the "top" output 

 

INPUT: 

 

- ``meminfo`` -- output of "top" 

 

OUTPUT: 

 

See :meth:`_parse_top`. 

 

TESTS:: 

 

sage: from sage.misc.memory_info import MemoryInfo_OSX 

sage: m = MemoryInfo_OSX.__new__(MemoryInfo_OSX) 

sage: osx_ppc = 'PhysMem: 64.7M wired, 87.3M active, 14.1M inactive, 29.3M used, 21.8M free' 

sage: m._parse_top_output(osx_ppc) 

{'available_ram': 22858956, 'total_ram': 53582232} 

 

sage: osx_x86 = 'PhysMem: 8861M wired, 3574M active, 678M inactive, 13G used, 19G free.' 

sage: m._parse_top_output(osx_x86) 

{'available_ram': 20401094656L, 'total_ram': 34359738368L} # 32-bit 

{'available_ram': 20401094656, 'total_ram': 34359738368} # 64-bit 

""" 

units = { 'K': 1024, 'M':1024**2, 'G':1024**3 } 

for line in meminfo.splitlines(): 

if not line.startswith('PhysMem:'): 

continue 

line = line.split() 

if not line[-1].startswith('free') or not line[-3].startswith('used'): 

raise OSError('failed to parse PhysMem: line in "top" output') 

used_ram = line[-4] 

free_ram = line[-2] 

used_ram = int( float(used_ram[:-1]) * units[used_ram[-1]]) 

free_ram = int( float(free_ram[:-1]) * units[free_ram[-1]]) 

return { 'total_ram': used_ram + free_ram, 

'available_ram': free_ram } 

raise OSError('failed to parse "top" output, no PhysMem: section') 

 

def _parse_top(self): 

""" 

Parse ``top`` output 

 

OUTPUT: 

 

A dictionary. All sizes are in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo, MemoryInfo_OSX 

sage: mem = MemoryInfo() 

sage: if isinstance(mem, MemoryInfo_OSX): 

....: info = mem._parse_top() 

....: else: 

....: info = None 

sage: info # random output 

{'available_ram': 1749782528, 

'total_ram': 16708194304} 

sage: keys = set(['available_ram', 'total_ram']) 

sage: (info is None) or (set(info.keys()) == keys) 

True 

""" 

import time 

if (time.time()-self._age) < self._maxage: 

return self._parse_top_cache 

meminfo = subprocess.check_output(['top', '-l', '1'], 

stderr=subprocess.STDOUT) 

result = self._parse_top_output(meminfo) 

self._age = time.time() 

self._parse_top_cache = result 

return result 

 

def total_ram(self): 

""" 

Return the total RAM size 

 

OUTPUT: 

 

Integer. The RAM size in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: MemoryInfo().total_ram() > 0 

True 

""" 

return self._parse_top()['total_ram'] 

 

def available_ram(self): 

""" 

Return the available (free) RAM size 

 

OUTPUT: 

 

Integer. The free RAM size in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: MemoryInfo().available_ram() > 0 

True 

""" 

return self._parse_top()['available_ram'] 

 

def total_swap(self): 

""" 

Return the total swap size 

 

The OSX swap file is growing dynamically, so we just return 

twice the total ram. 

 

OUTPUT: 

 

Integer. The swap size in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: MemoryInfo().total_swap() >= 0 

True 

""" 

return 2*self.total_ram() 

 

def available_swap(self): 

""" 

Return the available (free) swap size 

 

The OSX swap file is growing dynamically, so we just return 

twice the available ram. 

 

OUTPUT: 

 

Integer. The free swap size in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: MemoryInfo().available_swap() in ZZ 

True 

""" 

return 2*self.available_ram() 

 

 

class MemoryInfo_guess(MemoryInfo_base): 

""" 

Guess memory as a fallback. 

 

TESTS:: 

 

sage: from sage.misc.memory_info import MemoryInfo_guess 

sage: mem = MemoryInfo_guess() 

sage: mem.total_ram() 

4294967296 # 64-bit 

4294967296L # 32-bit 

""" 

def total_ram(self): 

""" 

Return the total RAM size 

 

OUTPUT: 

 

Integer. The RAM size in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: MemoryInfo().total_ram() > 0 

True 

""" 

GB = 1024 * 1024 * 1024 

return 4*GB 

 

def available_ram(self): 

""" 

Return the available (free) RAM size 

 

OUTPUT: 

 

Integer. The free RAM size in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: MemoryInfo().available_ram() > 0 

True 

""" 

return self.total_ram() 

 

def total_swap(self): 

""" 

Return the total swap size 

 

OUTPUT: 

 

Integer. The swap size in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: MemoryInfo().total_swap() >= 0 

True 

""" 

GB = 1024 * 1024 * 1024 

return 4*GB 

 

def available_swap(self): 

""" 

Return the available (free) swap size 

 

OUTPUT: 

 

Integer. The free swap size in bytes. 

 

EXAMPLES:: 

 

sage: from sage.misc.memory_info import MemoryInfo 

sage: MemoryInfo().available_swap() in ZZ 

True 

""" 

return self.total_swap()