Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[enhance] rusage: better way to compute memory usage on mac + added l…

…inux support
  • Loading branch information...
commit e4d51c4026b6070de055169da35a506570b78bc4 1 parent 45f8692
@Aqua-Ye Aqua-Ye authored
Showing with 44 additions and 8 deletions.
  1. +44 −8 libbase/rusage.c
View
52 libbase/rusage.c
@@ -15,17 +15,53 @@
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <sys/resource.h>
-long usage() {
- int who = RUSAGE_SELF;
- struct rusage usage;
- struct rusage *p = &usage;
- getrusage(who, p);
- return p->ru_maxrss;
+#include <caml/memory.h>
+
+#if (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) /* MAC */
+
+#include <mach/task.h>
+#include <mach/mach.h>
+
+// http://blog.kuriositaet.de/?p=257
+int get_mem (unsigned int *rss)
+{
+ struct task_basic_info t_info;
+ mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
+
+ if (KERN_SUCCESS != task_info(mach_task_self(),
+ TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count))
+ {
+ return -1;
+ }
+ *rss = t_info.resident_size;
+ return 0;
}
-#include <caml/memory.h>
+#else /* not MAC */
+
+#include <stdio.h>
+#include <unistd.h>
+
+int get_mem (unsigned int *rss)
+{
+ FILE* f = fopen("/proc/self/statm", "r");
+ unsigned int res = -1;
+ long page_size = getpagesize();
+ if (!f) return -1;
+ if (fscanf(f, "%d\n", &res) != 1) return -1;
+ *rss = res*page_size;
+ fclose(f);
+ return 0;
+}
+
+#endif
+
+long usage() {
+ unsigned int rss = -1;
+ get_mem(&rss);
+ return rss;
+}
value get_memory_usage() {
CAMLparam0();

0 comments on commit e4d51c4

Please sign in to comment.
Something went wrong with that request. Please try again.