FileDocCategorySizeDatePackage
ViewMemoryPanel.javaAPI DocphoneME MR2 API (J2ME)8966Wed May 02 17:59:48 BST 2007view

ViewMemoryPanel

public class ViewMemoryPanel extends JComponent

Fields Summary
MPDataProvider
_data_provider
private static final int
_hor_bloc_number
private static final int
_ver_bloc_number
private static final int
_block_number
private int
_block_height
private int
_block_width
private double[]
_block_utilization_count
private JavaClass
selected_class
private int[]
_block_object_count
private int
_inline_allocation_top
private int
_old_generation_end
private int
_heap_start
private int
_heap_top
Constructors Summary
public ViewMemoryPanel(MPDataProvider data_provider)


     
    _data_provider = data_provider;
    setMinimumSize(new Dimension(600, 300));
    addMouseListener(new ViewMemoryMouseListener());
  
Methods Summary
public booleancontains(int x, int y)

    if (!super.contains(x, y)) 
       return false;
    if (y > _block_height * _ver_bloc_number || x > _block_width * _hor_bloc_number)
      return false;
    int blockNum = (y / _block_height) * _hor_bloc_number + x / _block_width;
    int address = get_address_by_block_num(blockNum);
    setToolTipText("0x" + Integer.toHexString(address));
    return true;
  
public voiddraw_agenda(java.awt.Graphics g)

    int x = _block_width * _hor_bloc_number + 10;
    int y = 1;
    draw_block_symbol(g, x, y, 100, 0);
    draw_allocation_top_symbol(g, x, y, false);
    g.setColor(Color.BLACK);
    g.drawString("_inline_allocation_top block", x + 2*_block_width, y + _block_height );

    y += 2*_block_height;
    draw_block_symbol(g, x, y, 100, 0);
    draw_old_generation_end_symbol(g, x, y, false);
    g.setColor(Color.BLACK);
    g.drawString("_old_generation_end block", x + 2*_block_width, y + _block_height );

    y += 2*_block_height;
    draw_block_symbol(g, x, y, 1, 0);
    g.setColor(Color.BLACK);
    g.drawString("block with 100% utilization", x + 2*_block_width, y + _block_height );

    y += 2*_block_height;
    draw_block_symbol(g, x, y, 0.5, 0);
    g.setColor(Color.BLACK);
    g.drawString("block with 50% utilization", x + 2*_block_width, y + _block_height );

    y += 2*_block_height;
    draw_block_symbol(g, x, y, 0, 0);
    g.setColor(Color.BLACK);
    g.drawString("block with 0% utilization", x + 2*_block_width, y + _block_height );

    y += 2*_block_height;
    draw_block_symbol(g, x, y, 100, 1);
    g.setColor(Color.BLACK);
    g.drawString("block with object ", x + 2*_block_width, y + _block_height/2 );
    g.drawString("of selected type", x + 2*_block_width, y + (3*_block_height) / 2);

    y += 2*_block_height;
    draw_block_symbol(g, x, y, 100, 110);
    g.setColor(Color.BLACK);
    g.drawString("block with many objects ", x + 2*_block_width, y + _block_height / 2 );
    g.drawString("of selected type", x + 2*_block_width, y + (3*_block_height) / 2 );

  
private voiddraw_allocation_top_symbol(java.awt.Graphics g, int x, int y, boolean gc)

    g.setColor(new Color(164, 255, 64));    
    g.drawRect(x, y, 1, _block_height);      
  
private voiddraw_block(java.awt.Graphics g, int i)

    int x = _block_width * (i % _hor_bloc_number);
    int y = _block_height * (i / _hor_bloc_number);
    draw_block_symbol(g, x, y, _block_utilization_count[i], _block_object_count[i]);
  
private voiddraw_block_symbol(java.awt.Graphics g, int x, int y, double utilization, int objects_num)

    int util = (int)(255*Math.sqrt(1 - utilization));
    g.setColor(new Color(util, 0, 0));
    g.fillRect(x + 1, y + 1, _block_width  - 1, _block_height - 1);
    g.setColor(new Color(255, 255, 0));
    int radius = 0;
    if (objects_num > 0) radius = 1;
    if (objects_num > 10) radius = 2;
    if (objects_num > 100) { radius = 3; x--; }
    g.fillOval(x + _block_width / 2, y + _block_height / 2, radius, radius);
  
private voiddraw_inline_allocations(java.awt.Graphics g)

    int i = get_block_number(_inline_allocation_top);
    int x = _block_width * (i % _hor_bloc_number);
    int y = _block_height * (i / _hor_bloc_number);
    draw_allocation_top_symbol(g, x, y, false);
  
private voiddraw_old_generation_end(java.awt.Graphics g)

    int i = get_block_number(_old_generation_end);
    int x = _block_width * (i % _hor_bloc_number);
    int y = _block_height * (i / _hor_bloc_number);
    draw_old_generation_end_symbol(g, x, y, false);
  
private voiddraw_old_generation_end_symbol(java.awt.Graphics g, int x, int y, boolean gc)

    g.setColor(new Color(64, 64, 255));    
    g.drawRect(x, y, 1, _block_height);      
  
public java.awt.DimensiongetPreferredSize()

    return new Dimension(900, 300);
  
private intget_address_by_block_num(int block_num)

  
    return (int)(_heap_start + (_heap_top - _heap_start) * (((double)block_num) / _block_number));
  
private intget_block_num_by_mouse_event(java.awt.event.MouseEvent e)

    
    return e.getY() / _block_height * _hor_bloc_number + e.getX() / _block_width;       
  
private intget_block_number(int address)

    if (address < _heap_start || address > _heap_top) {
      return -1;
    }
    double tmp = ((double)(address - _heap_start)) /(_heap_top - _heap_start);
    int res = (int)(tmp * _block_number);
    return res;
  
public voidpaint(java.awt.Graphics g)

    Dimension size = getSize();
    _block_width = (size.width - 150) / _hor_bloc_number;
    _block_height = size.height / _ver_bloc_number;
    prepare_blocks();
    for (int i = 0; i < _block_number; i++) {
      draw_block(g, i);
    }
    draw_inline_allocations(g);
    draw_old_generation_end(g);
    draw_agenda(g);
  
private voidprepare_blocks()

    for (int i = 0; i < _block_number; i++) {
      _block_object_count[i] = 0;
    }

    JavaObject[] objects = _data_provider.getObjectsOfClass(selected_class);
    for (int i = 0; i < objects.length; i++) {
      int address = objects[i].address;
      int block_number = get_block_number(address);
      _block_object_count[block_number]++;
    }
  
public voidset_selected_class_id(JavaClass jc)

    selected_class = jc;
  
public voidupdate()

    _inline_allocation_top = _data_provider.get_allocation_top();
    _old_generation_end = _data_provider.get_old_gen_end();
    _heap_start = _data_provider.get_heap_start();
    _heap_top = _data_provider.get_heap_top();
    update_block_utilization();
    repaint();
  
private voidupdate_block_utilization()

    for (int i = 0; i < _block_number; i++) {      
      int start = get_address_by_block_num(i);
      int end = get_address_by_block_num(i+1);
      JavaObject[] objects = _data_provider.getObjectsFromTheAddresses(start, end);
      int full_size = 0;
      int real_size = 0;
      for (int j = 0; j < objects.length; j++) {
        full_size += objects[j].size;
        if (objects[j].alive()) real_size += objects[j].size;
      }
      if (full_size == 0) {
        _block_utilization_count[i] = 1;
      } else {
        _block_utilization_count[i] = ((double)real_size) / full_size;
      }
    }