diff options
author | Jacek Antonelli | 2008-08-15 23:45:11 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:45:11 -0500 |
commit | 215f423cbe18fe9ca14a26caef918d303bad28ff (patch) | |
tree | 0743442b286216cc8e19aa487c26f4e9345ffd64 /linden/indra/llcommon/llmemory.cpp | |
parent | Second Life viewer sources 1.18.3.5-RC (diff) | |
download | meta-impy-215f423cbe18fe9ca14a26caef918d303bad28ff.zip meta-impy-215f423cbe18fe9ca14a26caef918d303bad28ff.tar.gz meta-impy-215f423cbe18fe9ca14a26caef918d303bad28ff.tar.bz2 meta-impy-215f423cbe18fe9ca14a26caef918d303bad28ff.tar.xz |
Second Life viewer sources 1.18.4.0-RC
Diffstat (limited to '')
-rw-r--r-- | linden/indra/llcommon/llmemory.cpp | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/linden/indra/llcommon/llmemory.cpp b/linden/indra/llcommon/llmemory.cpp index 65f3409..e52a137 100644 --- a/linden/indra/llcommon/llmemory.cpp +++ b/linden/indra/llcommon/llmemory.cpp | |||
@@ -2,6 +2,8 @@ | |||
2 | * @file llmemory.cpp | 2 | * @file llmemory.cpp |
3 | * @brief Very special memory allocation/deallocation stuff here | 3 | * @brief Very special memory allocation/deallocation stuff here |
4 | * | 4 | * |
5 | * $LicenseInfo:firstyear=2002&license=viewergpl$ | ||
6 | * | ||
5 | * Copyright (c) 2002-2007, Linden Research, Inc. | 7 | * Copyright (c) 2002-2007, Linden Research, Inc. |
6 | * | 8 | * |
7 | * Second Life Viewer Source Code | 9 | * Second Life Viewer Source Code |
@@ -24,10 +26,26 @@ | |||
24 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | 26 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO |
25 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | 27 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, |
26 | * COMPLETENESS OR PERFORMANCE. | 28 | * COMPLETENESS OR PERFORMANCE. |
29 | * $/LicenseInfo$ | ||
27 | */ | 30 | */ |
28 | 31 | ||
29 | #include "linden_common.h" | 32 | #include "linden_common.h" |
30 | 33 | ||
34 | #if defined(LL_WINDOWS) | ||
35 | # include <windows.h> | ||
36 | # include <psapi.h> | ||
37 | #elif defined(LL_DARWIN) | ||
38 | # include <sys/types.h> | ||
39 | # include <sys/sysctl.h> | ||
40 | # include <mach/task.h> | ||
41 | # include <mach/vm_map.h> | ||
42 | # include <mach/mach_init.h> | ||
43 | # include <mach/vm_region.h> | ||
44 | # include <mach/mach_port.h> | ||
45 | #elif defined(LL_LINUX) | ||
46 | # include <unistd.h> | ||
47 | #endif | ||
48 | |||
31 | #include "llmemory.h" | 49 | #include "llmemory.h" |
32 | #include "llmemtype.h" | 50 | #include "llmemtype.h" |
33 | 51 | ||
@@ -278,3 +296,131 @@ LLRefCount::~LLRefCount() | |||
278 | 296 | ||
279 | //---------------------------------------------------------------------------- | 297 | //---------------------------------------------------------------------------- |
280 | 298 | ||
299 | #if defined(LL_WINDOWS) | ||
300 | |||
301 | U64 getCurrentRSS() | ||
302 | { | ||
303 | HANDLE self = GetCurrentProcess(); | ||
304 | PROCESS_MEMORY_COUNTERS counters; | ||
305 | |||
306 | if (!GetProcessMemoryInfo(self, &counters, sizeof(counters))) | ||
307 | { | ||
308 | llwarns << "GetProcessMemoryInfo failed" << llendl; | ||
309 | return 0; | ||
310 | } | ||
311 | |||
312 | return counters.WorkingSetSize; | ||
313 | } | ||
314 | |||
315 | #elif defined(LL_DARWIN) | ||
316 | |||
317 | static U32 getPageSize() | ||
318 | { | ||
319 | int ctl[2] = { CTL_HW, HW_PAGESIZE }; | ||
320 | int page_size; | ||
321 | size_t size = sizeof(page_size); | ||
322 | |||
323 | if (sysctl(ctl, 2, &page_size, &size, NULL, 0) == -1) | ||
324 | { | ||
325 | llwarns << "Couldn't get page size" << llendl; | ||
326 | return 0; | ||
327 | } else { | ||
328 | return page_size; | ||
329 | } | ||
330 | } | ||
331 | |||
332 | U64 getCurrentRSS() | ||
333 | { | ||
334 | task_t task = mach_task_self(); | ||
335 | vm_address_t addr = VM_MIN_ADDRESS; | ||
336 | vm_size_t size = 0; | ||
337 | U64 residentPages = 0; | ||
338 | |||
339 | while (true) | ||
340 | { | ||
341 | mach_msg_type_number_t bcount = VM_REGION_BASIC_INFO_COUNT; | ||
342 | vm_region_basic_info binfo; | ||
343 | mach_port_t bobj; | ||
344 | kern_return_t ret; | ||
345 | |||
346 | addr += size; | ||
347 | |||
348 | ret = vm_region(task, &addr, &size, VM_REGION_BASIC_INFO, | ||
349 | (vm_region_info_t) &binfo, &bcount, &bobj); | ||
350 | |||
351 | if (ret != KERN_SUCCESS) | ||
352 | { | ||
353 | break; | ||
354 | } | ||
355 | |||
356 | if (bobj != MACH_PORT_NULL) | ||
357 | { | ||
358 | mach_port_deallocate(task, bobj); | ||
359 | } | ||
360 | |||
361 | mach_msg_type_number_t ecount = VM_REGION_EXTENDED_INFO_COUNT; | ||
362 | vm_region_extended_info einfo; | ||
363 | mach_port_t eobj; | ||
364 | |||
365 | ret = vm_region(task, &addr, &size, VM_REGION_EXTENDED_INFO, | ||
366 | (vm_region_info_t) &einfo, &ecount, &eobj); | ||
367 | |||
368 | if (ret != KERN_SUCCESS) | ||
369 | { | ||
370 | llwarns << "vm_region failed" << llendl; | ||
371 | return 0; | ||
372 | } | ||
373 | |||
374 | if (eobj != MACH_PORT_NULL) | ||
375 | { | ||
376 | mach_port_deallocate(task, eobj); | ||
377 | } | ||
378 | |||
379 | residentPages += einfo.pages_resident; | ||
380 | } | ||
381 | |||
382 | return residentPages * getPageSize(); | ||
383 | } | ||
384 | |||
385 | #elif defined(LL_LINUX) | ||
386 | |||
387 | U64 getCurrentRSS() | ||
388 | { | ||
389 | static const char statPath[] = "/proc/self/stat"; | ||
390 | FILE *fp = fopen(statPath, "r"); | ||
391 | U64 rss = 0; | ||
392 | |||
393 | if (fp == NULL) | ||
394 | { | ||
395 | llwarns << "couldn't open " << statPath << llendl; | ||
396 | goto bail; | ||
397 | } | ||
398 | |||
399 | // Eee-yew! See Documentation/filesystems/proc.txt in your | ||
400 | // nearest friendly kernel tree for details. | ||
401 | |||
402 | { | ||
403 | int ret = fscanf(fp, "%*d (%*[^)]) %*c %*d %*d %*d %*d %*d %*d %*d " | ||
404 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %Lu", | ||
405 | &rss); | ||
406 | if (ret != 1) | ||
407 | { | ||
408 | llwarns << "couldn't parse contents of " << statPath << llendl; | ||
409 | rss = 0; | ||
410 | } | ||
411 | } | ||
412 | |||
413 | fclose(fp); | ||
414 | |||
415 | bail: | ||
416 | return rss; | ||
417 | } | ||
418 | |||
419 | #else | ||
420 | |||
421 | U64 getCurrentRSS() | ||
422 | { | ||
423 | return 0; | ||
424 | } | ||
425 | |||
426 | #endif | ||