001 /** 002 ############################################################################## 003 ## ## 004 ## EnvInfo ## 005 ## ## 006 ## Copyright (C) 2009 Frederic Roudaut <frederic.roudaut@free.fr> ## 007 ## ## 008 ## ## 009 ## This program is free software: you can redistribute it and/or modify ## 010 ## it under the terms of the GNU General Public License as published by ## 011 ## the Free Software Foundation, either version 3 of the License, or ## 012 ## (at your option) any later version. ## 013 ## ## 014 ## This program is distributed in the hope that it will be useful, ## 015 ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## 016 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## 017 ## GNU General Public License for more details. ## 018 ## ## 019 ## You should have received a copy of the GNU General Public License ## 020 ## along with this program. If not, see <http://www.gnu.org/licenses/>. ## 021 ## ## 022 ## ## 023 ############################################################################## 024 **/ 025 026 027 028 package com.envInfo; 029 030 import java.awt.BorderLayout; 031 import java.awt.GridBagLayout; 032 import java.awt.GridBagConstraints; 033 import java.awt.event.*; 034 import java.awt.Insets; 035 import java.awt.Font; 036 import java.awt.Dimension; 037 import java.awt.Color; 038 import javax.swing.*; 039 import javax.swing.event.*; 040 041 import com.awt.GridBagLayoutConstraint; 042 043 import java.util.*; 044 045 import java.lang.management.ManagementFactory; 046 import java.lang.management.OperatingSystemMXBean; 047 import java.lang.management.ThreadMXBean; 048 import java.lang.management.ThreadInfo; 049 050 051 052 /** 053 * A Class for showing in real time some System (OS, CPU, Java Memory) Info. 054 * 055 */ 056 public class SystemInfo extends JFrame 057 { 058 059 private static final long serialVersionUID = 1L; 060 061 protected JTextField totalMemoryJText; 062 protected JTextField freeMemoryJText; 063 protected JTextField maxMemoryJText; 064 protected JTextField usedMemoryJText; 065 protected JComboBox outputUnit; 066 protected float memoryTimer = 1.0f; // Timer to update memory info (sec) 067 protected float threadsTimer = 60.0f; // Timer to update threads info (sec) 068 protected MemoryChartPanel chartPanel = null; 069 protected long chartDateStart=0; 070 protected JPanel threadsTextPanel; 071 072 /** 073 * Draw a Frame with System (OS, CPU, Java Memory) Info. 074 * 075 */ 076 public SystemInfo() 077 { 078 super(Info.ressources.getObject("System_Info").toString()); 079 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 080 setVisible(true); 081 082 /** 083 * Class containing a thread in order to update the current Memory Info . 084 * 085 */ 086 class updateMemoryInfo extends Thread 087 { 088 /** 089 * Start a thread for updating Memory Info every 1s. 090 * 091 */ 092 public void run() { 093 while(true) 094 { 095 try { 096 Thread.sleep(Math.round(memoryTimer * 1000)); 097 } 098 catch (Exception e) 099 { 100 } 101 updateMemoryInfo(); 102 } 103 } 104 } 105 106 final updateMemoryInfo mem = new updateMemoryInfo(); 107 Runtime r = Runtime.getRuntime(); 108 OperatingSystemMXBean bean = ManagementFactory.getOperatingSystemMXBean(); 109 110 111 JPanel systemInfoBoxPanel = new JPanel(new BorderLayout()); 112 getContentPane().add(systemInfoBoxPanel, BorderLayout.CENTER); 113 JLabel entryText; 114 115 JPanel textPanel, textPanel2 ; 116 textPanel = new JPanel(); 117 GridBagLayout gbMain = new GridBagLayout(); 118 textPanel.setLayout(gbMain); 119 systemInfoBoxPanel.add(textPanel, BorderLayout.WEST); 120 121 textPanel2 = new JPanel(); 122 GridBagLayout gb = new GridBagLayout(); 123 textPanel2.setLayout(gb); 124 textPanel2.setBorder(BorderFactory.createTitledBorder(Info.ressources.getObject("System").toString())); 125 126 if (bean != null) 127 { 128 // Returns the System architecture. 129 entryText = new JLabel(Info.ressources.getObject("System_architecture").toString()); 130 entryText.setFont(new Font("SansSerif", Font.BOLD, 11)); 131 textPanel2.add(entryText); 132 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,0, 133 new Insets(15,8,0,15))); 134 135 136 entryText = new JLabel("= " + bean.getArch()); 137 entryText.setFont(new Font("SansSerif", Font.PLAIN, 10)); 138 textPanel2.add(entryText); 139 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,2,0, 140 new Insets(15,8,0,15))); 141 142 /* 143 entryText = new JLabel(Utils.ressources.getObject("CPU").toString()); 144 entryText.setFont(new Font("SansSerif", Font.BOLD, 11)); 145 textPanel2.add(entryText); 146 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,1, 147 new Insets(0,8,0,15))); 148 149 150 entryText = new JLabel("= " + System.getProperty("sun.cpu.isalist")); 151 entryText.setFont(new Font("SansSerif", Font.PLAIN, 10)); 152 textPanel2.add(entryText); 153 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,2,1, 154 new Insets(0,8,0,15))); 155 156 */ 157 // Returns the Operating system name 158 entryText = new JLabel(Info.ressources.getObject("Operating_System_Name").toString()); 159 entryText.setFont(new Font("SansSerif", Font.BOLD, 11)); 160 textPanel2.add(entryText); 161 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,2, 162 new Insets(0,8,0,15))); 163 164 165 entryText = new JLabel("= " + bean.getName()); 166 entryText.setFont(new Font("SansSerif", Font.PLAIN, 10)); 167 textPanel2.add(entryText); 168 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,2,2, 169 new Insets(0,8,0,15))); 170 171 // Returns the Operating system version 172 entryText = new JLabel(Info.ressources.getObject("Operating_System_Version").toString()); 173 entryText.setFont(new Font("SansSerif", Font.BOLD, 11)); 174 textPanel2.add(entryText); 175 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,3, 176 new Insets(0,8,0,15))); 177 178 179 entryText = new JLabel("= " + bean.getVersion()); 180 entryText.setFont(new Font("SansSerif", Font.PLAIN, 10)); 181 textPanel2.add(entryText); 182 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,2,3, 183 new Insets(0,8,0,15))); 184 } 185 186 // Returns the number of processors available to the Java virtual machine. 187 entryText = new JLabel(Info.ressources.getObject("Available_Processors").toString()); 188 entryText.setFont(new Font("SansSerif", Font.BOLD, 11)); 189 textPanel2.add(entryText); 190 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,4, 191 new Insets(20,8,0,15))); 192 193 entryText = new JLabel("= " + r.availableProcessors()); 194 entryText.setFont(new Font("SansSerif", Font.PLAIN, 10)); 195 textPanel2.add(entryText); 196 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,2,4, 197 new Insets(20,8,0,15))); 198 199 200 /* 201 if (bean != null) 202 { 203 try { 204 // Returns the System load average for the last minute (Java 6 only) 205 double i = bean.getSystemLoadAverage(); 206 if (i > 0) 207 { 208 entryText = new JLabel(Utils.ressources.getObject("System_Load").toString()); 209 entryText.setFont(new Font("SansSerif", Font.BOLD, 11)); 210 textPanel2.add(entryText); 211 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,5, 212 new Insets(0,8,0,15))); 213 214 215 entryText = new JLabel("= " + i); 216 entryText.setFont(new Font("SansSerif", Font.PLAIN, 10)); 217 textPanel2.add(entryText); 218 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,2,5, 219 new Insets(0,8,0,15))); 220 } 221 222 } 223 catch (Exception e) 224 { 225 } 226 227 } 228 */ 229 230 textPanel.add(textPanel2); 231 gbMain.setConstraints(textPanel2, new GridBagLayoutConstraint(GridBagConstraints.WEST,0,0, 232 new Insets(8,8,0,8))); 233 234 textPanel2 = new JPanel(); 235 gb = new GridBagLayout(); 236 textPanel2.setLayout(gb); 237 textPanel2.setBorder(BorderFactory.createTitledBorder(Info.ressources.getObject("Memory").toString())); 238 239 // compute the different memory in MB 240 chartDateStart = (new Date()).getTime() / 1000; 241 float totalMemory = r.totalMemory() / (1024 * 1024); 242 float freeMemory = r.freeMemory() / (1024 * 1024); 243 float maxMemory = r.maxMemory() / (1024 * 1024); 244 float usedMemory = totalMemory - freeMemory;; 245 246 // Returns the maximum amount of memory that the Java virtual machine will attempt to use. 247 entryText = new JLabel(Info.ressources.getObject("Max_Memory").toString()); 248 entryText.setFont(new Font("SansSerif", Font.BOLD, 11)); 249 textPanel2.add(entryText); 250 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,6, 251 new Insets(15,8,0,15))); 252 253 maxMemoryJText = new JTextField(Float.valueOf((float)Math.round(maxMemory * 100) / 100).toString()); 254 maxMemoryJText.setFont(new Font("SansSerif", Font.PLAIN, 10)); 255 maxMemoryJText.setColumns(10); 256 maxMemoryJText.setEditable(false); 257 maxMemoryJText.setHorizontalAlignment(JTextField.RIGHT); 258 textPanel2.add(maxMemoryJText); 259 gb.setConstraints(maxMemoryJText, new GridBagLayoutConstraint(GridBagConstraints.WEST,2,6, 260 new Insets(15,8,0,15))); 261 262 // Returns the total amount of memory in the Java virtual machine. 263 entryText = new JLabel(Info.ressources.getObject("Total_Memory").toString()); 264 entryText.setFont(new Font("SansSerif", Font.BOLD, 11)); 265 textPanel2.add(entryText); 266 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,7, 267 new Insets(0,8,0,15))); 268 269 totalMemoryJText = new JTextField(Float.valueOf((float)Math.round(totalMemory * 100) / 100).toString()); 270 totalMemoryJText.setFont(new Font("SansSerif", Font.PLAIN, 10)); 271 totalMemoryJText.setColumns(10); 272 totalMemoryJText.setEditable(false); 273 totalMemoryJText.setHorizontalAlignment(JTextField.RIGHT); 274 textPanel2.add(totalMemoryJText); 275 gb.setConstraints(totalMemoryJText, new GridBagLayoutConstraint(GridBagConstraints.WEST,2,7, 276 new Insets(0,8,0,15))); 277 278 // Returns the amount of free memory in the Java Virtual Machine. 279 entryText = new JLabel(Info.ressources.getObject("Free_Memory").toString()); 280 entryText.setFont(new Font("SansSerif", Font.BOLD, 11)); 281 textPanel2.add(entryText); 282 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,8, 283 new Insets(0,8,0,15))); 284 285 freeMemoryJText = new JTextField(Float.valueOf((float)Math.round(freeMemory * 100) / 100).toString()); 286 freeMemoryJText.setFont(new Font("SansSerif", Font.PLAIN, 10)); 287 freeMemoryJText.setColumns(10); 288 freeMemoryJText.setEditable(false); 289 freeMemoryJText.setHorizontalAlignment(JTextField.RIGHT); 290 textPanel2.add(freeMemoryJText); 291 gb.setConstraints(freeMemoryJText, new GridBagLayoutConstraint(GridBagConstraints.WEST,2,8, 292 new Insets(0,8,0,15))); 293 294 // Returns the amount of used memory in the Java Virtual Machine. 295 entryText = new JLabel(Info.ressources.getObject("Used_Memory").toString()); 296 entryText.setFont(new Font("SansSerif", Font.BOLD, 11)); 297 textPanel2.add(entryText); 298 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,9, 299 new Insets(0,8,0,15))); 300 301 302 usedMemoryJText = new JTextField(Float.valueOf((float)Math.round(usedMemory * 100) / 100).toString()); 303 usedMemoryJText.setFont(new Font("SansSerif", Font.PLAIN, 10)); 304 usedMemoryJText.setColumns(10); 305 usedMemoryJText.setEditable(false); 306 usedMemoryJText.setHorizontalAlignment(JTextField.RIGHT); 307 textPanel2.add(usedMemoryJText); 308 gb.setConstraints(usedMemoryJText, new GridBagLayoutConstraint(GridBagConstraints.WEST,2,9, 309 new Insets(0,8,0,15))); 310 311 // Set the output Unit 312 entryText = new JLabel(Info.ressources.getObject("Output_Unit").toString()); 313 entryText.setFont(new Font("SansSerif", Font.BOLD, 11)); 314 textPanel2.add(entryText); 315 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,10, 316 new Insets(0,8,0,15))); 317 318 String [] outputUnitValues = {Info.ressources.getObject("Bytes").toString(), 319 Info.ressources.getObject("KB").toString(), 320 Info.ressources.getObject("MB").toString()}; 321 outputUnit = new JComboBox(outputUnitValues); 322 outputUnit.setFont(new Font("SansSerif", Font.PLAIN, 10)); 323 outputUnit.setSelectedIndex(2); 324 outputUnit.addActionListener(new ActionListener() { 325 public void actionPerformed(ActionEvent e) { 326 chartPanel.reconfigure(Info.ressources.getObject("Memory_Status").toString(), 327 (String)outputUnit.getSelectedItem(), 328 Info.ressources.getObject("Time").toString(), 329 Info.ressources.getObject("Memory").toString() + " (" 330 + (String)outputUnit.getSelectedItem() + ")", 120.0); 331 chartPanel.maybeAddNewSeries("0", Info.ressources.getObject("Total_Memory2").toString() +": ", 332 SeriesColorGenerator.nextColor()); 333 chartPanel.maybeAddNewSeries("1", Info.ressources.getObject("Used_Memory2").toString() + ": ", 334 SeriesColorGenerator.nextColor()); 335 chartPanel.maybeAddNewSeries("2", Info.ressources.getObject("Free_Memory2").toString() + ": ", 336 SeriesColorGenerator.nextColor()); 337 chartPanel.start(); 338 chartDateStart = (new Date()).getTime() / 1000; 339 updateMemoryInfo(); 340 pack(); 341 } 342 }); 343 344 textPanel2.add(outputUnit); 345 gb.setConstraints(outputUnit, new GridBagLayoutConstraint(GridBagConstraints.WEST,2,10, 346 new Insets(0,8,0,15))); 347 textPanel.add(textPanel2); 348 gbMain.setConstraints(textPanel2, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,0, 349 new Insets(8,8,0,8))); 350 textPanel2 = new JPanel(); 351 gb = new GridBagLayout(); 352 textPanel2.setLayout(gb); 353 354 // Set the memory update Timer 355 entryText = new JLabel(Info.ressources.getObject("Memory_Timer").toString()); 356 entryText.setFont(new Font("SansSerif", Font.BOLD, 11)); 357 textPanel2.add(entryText); 358 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,0,0, 359 new Insets(0,8,0,8))); 360 361 SpinnerModel memoryTimerSpinnerModel = new SpinnerNumberModel(Float.valueOf(memoryTimer), //initial value 362 Float.valueOf(0.5f), //min 363 Float.valueOf(60.0f), //max 364 Float.valueOf(0.5f)); //step 365 final JSpinner memoryTimerSpinner = new JSpinner(memoryTimerSpinnerModel); 366 ((JSpinner.DefaultEditor)memoryTimerSpinner.getEditor()).getTextField().setColumns(3); 367 memoryTimerSpinner.addChangeListener(new ChangeListener() { 368 public void stateChanged(ChangeEvent e) 369 { 370 memoryTimer = (Float)memoryTimerSpinner.getValue(); 371 } 372 } 373 ); 374 375 memoryTimerSpinner.setFont(new Font("SansSerif", Font.PLAIN, 10)); 376 textPanel2.add(memoryTimerSpinner); 377 gb.setConstraints(memoryTimerSpinner, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,0, 378 new Insets(0,8,0,8))); 379 380 // Set the threads update Timer 381 entryText = new JLabel(Info.ressources.getObject("Threads_Timer").toString()); 382 entryText.setFont(new Font("SansSerif", Font.BOLD, 11)); 383 textPanel2.add(entryText); 384 gb.setConstraints(entryText, new GridBagLayoutConstraint(GridBagConstraints.WEST,0,1, 385 new Insets(0,8,0,8))); 386 387 SpinnerModel threadsTimerSpinnerModel = new SpinnerNumberModel(Float.valueOf(threadsTimer), //initial value 388 Float.valueOf(0.5f), //min 389 Float.valueOf(3600.0f), //max 390 Float.valueOf(0.5f)); //step 391 final JSpinner threadsTimerSpinner = new JSpinner(threadsTimerSpinnerModel); 392 ((JSpinner.DefaultEditor)threadsTimerSpinner.getEditor()).getTextField().setColumns(3); 393 threadsTimerSpinner.addChangeListener(new ChangeListener() { 394 public void stateChanged(ChangeEvent e) 395 { 396 threadsTimer = (Float)threadsTimerSpinner.getValue(); 397 } 398 } 399 ); 400 401 threadsTimerSpinner.setFont(new Font("SansSerif", Font.PLAIN, 10)); 402 textPanel2.add(threadsTimerSpinner); 403 gb.setConstraints(threadsTimerSpinner, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,1, 404 new Insets(0,8,0,8))); 405 406 textPanel.add(textPanel2); 407 gbMain.setConstraints(textPanel2, new GridBagLayoutConstraint(GridBagConstraints.WEST,0,1, 408 new Insets(8,8,0,8))); 409 410 // Add the Memory Chart Panel 411 chartPanel = new MemoryChartPanel(Info.ressources.getObject("Memory_Status").toString(), 412 (String)outputUnit.getSelectedItem(), 413 Info.ressources.getObject("Time").toString(), 414 Info.ressources.getObject("Memory").toString() + " (" 415 + (String)outputUnit.getSelectedItem() + ")", 416 memoryTimer, 120.0, 417 Color.black, Color.white, Color.gray); 418 chartPanel.maybeAddNewSeries("0", Info.ressources.getObject("Total_Memory2").toString() + ": ", 419 SeriesColorGenerator.nextColor()); 420 chartPanel.maybeAddNewSeries("1", Info.ressources.getObject("Used_Memory2").toString() + ": ", 421 SeriesColorGenerator.nextColor()); 422 chartPanel.maybeAddNewSeries("2", Info.ressources.getObject("Free_Memory2").toString() + ": ", 423 SeriesColorGenerator.nextColor()); 424 chartPanel.start(); 425 MemoryMeasurement M; 426 427 M = new MemoryMeasurement(0, 0, 428 Double.valueOf(r.totalMemory()/(1024*1024)), 429 (String)outputUnit.getSelectedItem()); 430 chartPanel.addSeriesMemoryMeasurement("0", M); 431 M = new MemoryMeasurement(0, 0, 432 Double.valueOf((r.totalMemory() - r.freeMemory())/(1024*1024)), 433 (String)outputUnit.getSelectedItem()); 434 chartPanel.addSeriesMemoryMeasurement("1", M); 435 M = new MemoryMeasurement(0, 0, 436 Double.valueOf(r.freeMemory()/(1024*1024)), 437 (String)outputUnit.getSelectedItem()); 438 chartPanel.addSeriesMemoryMeasurement("2", M); 439 440 textPanel.add(chartPanel); 441 GridBagConstraints gbc = new GridBagLayoutConstraint(GridBagConstraints.WEST,0,2, 442 new Insets(15,8,0,8)); 443 gbc.gridwidth = 2; 444 gbMain.setConstraints(chartPanel, gbc); 445 446 textPanel2 = new JPanel(); 447 gb = new GridBagLayout(); 448 textPanel2.setLayout(gb); 449 450 JButton threadsbutton = new JButton("Threads"); 451 textPanel2.add(threadsbutton); 452 gb.setConstraints(threadsbutton, new GridBagLayoutConstraint(GridBagConstraints.WEST,0,0, 453 new Insets(0,0,0,2))); 454 threadsbutton.addActionListener(new ActionListener() { 455 public void actionPerformed(ActionEvent e) { 456 showJavaThreadsInfo(); 457 }}); 458 459 460 // Garbage Collector call 461 JButton gcbutton = new JButton("GC"); 462 textPanel2.add(gcbutton); 463 gb.setConstraints(gcbutton, new GridBagLayoutConstraint(GridBagConstraints.WEST,1,0, 464 new Insets(0,2,0,0))); 465 gcbutton.addActionListener(new ActionListener() { 466 public void actionPerformed(ActionEvent e) { 467 Runtime r = Runtime.getRuntime(); 468 r.gc(); 469 updateMemoryInfo(); 470 } 471 }); 472 473 474 textPanel.add(textPanel2); 475 gbMain.setConstraints(textPanel2, new GridBagLayoutConstraint(GridBagConstraints.WEST,0,3, 476 new Insets(15,8,8,8))); 477 478 JButton okbutton = new JButton(Info.ressources.getObject("OK").toString()); 479 textPanel.add(okbutton); 480 gbMain.setConstraints(okbutton, new GridBagLayoutConstraint(GridBagConstraints.EAST,1,3, 481 new Insets(15,8,8,8))); 482 okbutton.addActionListener(new ActionListener() { 483 public void actionPerformed(ActionEvent e) { 484 mem.stop(); 485 setVisible(false); 486 dispose(); 487 } 488 }); 489 490 mem.start(); 491 492 pack(); 493 setResizable(false); 494 setVisible(true); 495 } 496 497 498 499 /** 500 * Update the current Memory Info. 501 * 502 */ 503 private void updateMemoryInfo() 504 { 505 int unit = outputUnit.getSelectedIndex(); 506 Runtime r = Runtime.getRuntime(); 507 float totalMemory = r.totalMemory(); 508 float freeMemory = r.freeMemory(); 509 float maxMemory = r.maxMemory(); 510 float usedMemory = 0; 511 512 switch (unit) 513 { 514 case 0 : 515 // Already in bytes 516 break; 517 518 case 1 : 519 // In KBytes 520 totalMemory = totalMemory / 1024; 521 freeMemory = freeMemory / 1024; 522 maxMemory = maxMemory / 1024; 523 break; 524 525 case 2 : 526 // In MBytes 527 totalMemory = totalMemory / (1024 * 1024); 528 freeMemory = freeMemory / (1024 * 1024); 529 maxMemory = maxMemory / (1024 * 1024); 530 break; 531 532 default : 533 // Leaves in bytes 534 break; 535 } 536 537 totalMemoryJText.setText(Float.valueOf((float)Math.round(totalMemory * 100) / 100).toString()); 538 freeMemoryJText.setText(Float.valueOf((float)Math.round(freeMemory * 100) / 100).toString()); 539 maxMemoryJText.setText(Float.valueOf((float)Math.round(maxMemory * 100) / 100).toString()); 540 usedMemory = totalMemory - freeMemory; 541 usedMemoryJText.setText(Float.valueOf((float)Math.round(usedMemory * 100) / 100).toString()); 542 543 long ts = (new Date()).getTime() / 1000; 544 MemoryMeasurement M; 545 M = new MemoryMeasurement(ts - chartDateStart, ts - chartDateStart, totalMemory , (String)outputUnit.getSelectedItem()); 546 chartPanel.addSeriesMemoryMeasurement("0", M); 547 M = new MemoryMeasurement(ts - chartDateStart, ts - chartDateStart, usedMemory, (String)outputUnit.getSelectedItem()); 548 chartPanel.addSeriesMemoryMeasurement("1", M); 549 M = new MemoryMeasurement(ts - chartDateStart, ts - chartDateStart, freeMemory, (String)outputUnit.getSelectedItem()); 550 chartPanel.addSeriesMemoryMeasurement("2", M); 551 } 552 553 /** 554 * Draw a Frame with the Threads Info. 555 * 556 */ 557 public void showJavaThreadsInfo() 558 { 559 showJavaThreadsInfo(new Dimension(650, 800)); 560 } 561 562 /** 563 * Draw a Frame with the Threads Info. 564 * @param d the text Panel Dimension 565 * 566 */ 567 public void showJavaThreadsInfo(Dimension d) 568 { 569 /** 570 * Class containing a thread in order to update the current Threads Info . 571 * 572 */ 573 final Dimension PanelSize = d; 574 575 class updateJavaThreadsInfo extends Thread 576 { 577 /** 578 * Start a thread for updating Threads Info every 1s. 579 * 580 */ 581 public void run() { 582 while(true) 583 { 584 try { 585 Thread.sleep(Math.round(threadsTimer * 1000)); 586 } 587 catch (Exception e) 588 { 589 } 590 updateJavaThreadsInfo(PanelSize); 591 } 592 } 593 } 594 595 final updateJavaThreadsInfo threads = new updateJavaThreadsInfo(); 596 final JFrame threadsFrame = new JFrame(Info.ressources.getObject("Threads_Info").toString()); 597 threadsFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 598 599 threadsTextPanel = new JPanel(); 600 JPanel threadsPanel = new JPanel(); 601 GridBagLayout gb = new GridBagLayout(); 602 threadsPanel.setLayout(gb); 603 updateJavaThreadsInfo(PanelSize); 604 threadsPanel.add(threadsTextPanel); 605 gb.setConstraints(threadsTextPanel, new GridBagLayoutConstraint(GridBagConstraints.WEST,0,0, 606 new Insets(15,15,15,15))); 607 608 JButton okbutton = new JButton(Info.ressources.getObject("OK").toString()); 609 threadsPanel.add(okbutton); 610 gb.setConstraints(okbutton, new GridBagLayoutConstraint(GridBagConstraints.EAST,0,1, 611 new Insets(15,8,8,8))); 612 okbutton.addActionListener(new ActionListener() { 613 public void actionPerformed(ActionEvent e) { 614 threads.stop(); 615 threadsFrame.setVisible(false); 616 threadsFrame.dispose(); 617 } 618 }); 619 620 threadsFrame.getContentPane().add(threadsPanel); 621 threadsFrame.setResizable(false); 622 threadsFrame.pack(); 623 threadsFrame.setVisible(true); 624 threads.start(); 625 } 626 627 628 /** 629 * Update the current Java threads Info. 630 * 631 */ 632 protected void updateJavaThreadsInfo(Dimension d) 633 { 634 JTextArea text = new JTextArea(); 635 text.setFont(new Font("SansSerif", Font.PLAIN, 12)); 636 ThreadMXBean bean = ManagementFactory.getThreadMXBean(); 637 if (!bean.isThreadCpuTimeSupported()) { 638 System.err.println("Sorry, the Java virtual machine implementation does not support CPU time measurement for any thread."); 639 return; 640 } 641 if (!bean.isCurrentThreadCpuTimeSupported()) { 642 System.err.println("Sorry, JVM does not supports CPU time measurement for the current thread"); 643 return; 644 } 645 if (!bean.isThreadCpuTimeEnabled()) { 646 System.err.println("Sorry, Thread CPU measurement is not enabled"); 647 return; 648 } 649 650 String [] threadsTab = new String [bean.getThreadCount()]; 651 String textContent = ""; 652 int cpt = 0; 653 654 for (long id : bean.getAllThreadIds()) { 655 656 String idth =""; 657 if(id < 100) 658 idth += " "; 659 if(id < 10) 660 idth += " "; 661 idth += id; 662 663 ThreadInfo info = bean.getThreadInfo(id); 664 665 threadsTab[cpt] = " ID " + idth + ":\n java thread name: " + info.getThreadName() 666 + "\n java thread state: " + info.getThreadState() 667 + "\n Thread CPU Time : "+ bean.getThreadCpuTime(id) 668 + "\n Thread peak time: "+bean.getPeakThreadCount() 669 +"\n"; 670 671 cpt ++; 672 } 673 674 JScrollPane textScrolled = new JScrollPane(text); 675 textScrolled.setPreferredSize(d); 676 Arrays.sort(threadsTab); 677 for(int i=0; i < cpt; i++) 678 { 679 textContent += threadsTab[i] + "\n"; 680 } 681 682 text.append(textContent); 683 text.setCaretPosition(0); 684 685 //remove all components in panel. 686 threadsTextPanel.removeAll(); 687 threadsTextPanel.setPreferredSize(d); 688 // refresh the panel. 689 threadsTextPanel.add(textScrolled); 690 threadsTextPanel.updateUI(); 691 } 692 693 }