~tapaal-contributor/tapaal/autodetect-lens-check2

« back to all changes in this revision

Viewing changes to src/dk/aau/cs/util/MemoryMonitor.java

merged in lp:~yrke/tapaal/evenMore-gui-cleanup doing GUI cleanup and fixing memory access check, about menu in Mac, exception in verification and reenabled quick draw

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
package dk.aau.cs.util;
2
2
 
3
3
import com.sun.jna.*;
 
4
import dk.aau.cs.debug.Logger;
4
5
 
5
6
import java.io.BufferedReader;
 
7
import java.io.IOException;
6
8
import java.io.InputStreamReader;
7
9
import java.lang.reflect.Field;
8
10
import java.text.DecimalFormat;
14
16
 
15
17
public class MemoryMonitor {
16
18
 
17
 
        interface Kernel32 extends Library {
18
 
 
19
 
                static Kernel32 INSTANCE = Native.loadLibrary("kernel32", Kernel32.class);
20
 
 
21
 
                int GetProcessId(Long hProcess);
22
 
        }
23
 
 
24
 
        private static int PID = -1;
 
19
        private static long PID = -1;
25
20
        private static Semaphore busy = new Semaphore(1);
26
21
        private static double peakMemory = -1;
27
22
        private static Boolean cumulativePeakMemory = false;
37
32
        }
38
33
 
39
34
        public static void attach(Process p){
40
 
                PID = getPid(p);
 
35
                PID = p.pid();
41
36
                
42
37
                if( ! cumulativePeakMemory) {
43
38
                        peakMemory = -1;
79
74
                                        if(m.matches()){
80
75
                                                memory = Double.parseDouble(m.group(1).replace(".", "").replace(",", ""))/1024;
81
76
                                        }
82
 
                                } catch (Exception e) { 
 
77
                                } catch (IOException e) {
 
78
                                    Logger.log(e);
83
79
                                } 
84
80
                        }else{
85
 
                                try { 
 
81
                                try {
86
82
                                        Process p = Runtime.getRuntime().exec("ps -p "+PID+" -o rss"); 
87
83
                                        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); 
88
 
                                        
89
 
                                        String s = input.readLine();
90
 
                                        s = input.readLine();   //Actual memory usage is second line output
 
84
 
 
85
                                        input.readLine(); //Actual memory usage is second line output
 
86
                    String s = input.readLine();
91
87
                                        memory = Double.parseDouble(s.replace(" ", ""))/1024;
92
 
                                } catch (Exception e) { 
 
88
                                } catch (IOException e) {
 
89
                    Logger.log(e);
93
90
                                } 
94
91
                        }
95
92
 
105
102
                }
106
103
        }
107
104
 
108
 
        private static int getPid(Process p) {
109
 
                Field f;
110
 
 
111
 
                try{
112
 
                        if (Platform.isWindows()) {
113
 
                                f = p.getClass().getDeclaredField("handle");
114
 
                                f.setAccessible(true);
115
 
                return Kernel32.INSTANCE.GetProcessId((Long) f.get(p));
116
 
                        } else {
117
 
                                f = p.getClass().getDeclaredField("pid");
118
 
                                f.setAccessible(true);
119
 
                return (int) (Integer) f.get(p);
120
 
                        }
121
 
                }catch(Exception e){
122
 
                        return -1;
123
 
                }
124
 
        }
125
 
        
126
105
        public static String getPeakMemory(){
127
106
                return peakMemory == -1? "N/A":getFormatter().format(Math.ceil(peakMemory)) + " MB";
128
107
        }