]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.nativemem/src/org/simantics/nativemem/internal/linux/Stat.java
Rudimentary Linux and MacOSX support for org.simantics.nativemem
[simantics/platform.git] / bundles / org.simantics.nativemem / src / org / simantics / nativemem / internal / linux / Stat.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.nativemem.internal.linux;
13
14 import java.io.BufferedReader;
15 import java.io.IOException;
16 import java.nio.charset.Charset;
17 import java.nio.file.Files;
18 import java.nio.file.Paths;
19
20 import org.simantics.nativemem.internal.Resource;
21
22 /**
23  * @author Tuukka Lehtonen
24  * @since 1.29.0
25  */
26 public class Stat {
27
28         private static final String SELF_STATM = "/proc/self/statm";
29         private static final Charset ASCII = Charset.forName("US-ASCII");
30
31         public static long getCurrentRSS(Resource r) {
32                 try (BufferedReader rdr = Files.newBufferedReader(Paths.get(SELF_STATM), ASCII)) {
33                         String line = rdr.readLine();
34                         String[] nums = line.split(" ");
35                         if (nums.length == 0)
36                                 return 0L;
37                         long rss = Long.parseLong(nums[0]);
38                         long pageSize = r.sysconf(Resource._SC_PAGESIZE);
39                         return rss * pageSize;
40                 } catch (NumberFormatException e) {
41                         return 0L;
42                 } catch (IOException e) {
43                         return 0L;
44                 }
45         }
46
47 }