~nathwill-deactivatedaccount-deactivatedaccount/ubuntu/precise/unity/lp-972247

« back to all changes in this revision

Viewing changes to libunity/perf-logger.vala

  • Committer: Gordon Allott
  • Date: 2009-12-18 11:42:37 UTC
  • mfrom: (44.1.8 unity.instrumented)
  • Revision ID: mail@gordallott.com-20091218114237-iol6shag1dh3ruvz
Adds instrumentation and bootcharting support to unity

Instrumentation is enabled by passing --enable-testing to configure and
logging is enabled by passing --enable-boot-logging=foobar.log to unity.

you can then process the log file with ./tools/makebootchart.py --input=foobar.log --output=foobar.svg

instrumenting the code is enabled by decorating a function with START_FUNCTION (); and END_FUNCTION (); or by decorating a smaller process with LOGGING_START_PROCESS (process_name); and LOGGING_END_PROCESS (process_name);

all process names *must* be unique thoughout the system

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by Gord Allott <gord.allott@canonical.com>
 
17
 *
 
18
 */
 
19
 
 
20
namespace Unity
 
21
{
 
22
  
 
23
  public class ProcessInfo
 
24
  {
 
25
    public ProcessInfo (string name)
 
26
    {
 
27
      this.name = name;
 
28
      start = 0;
 
29
      end = 0;
 
30
    }
 
31
    public string name;
 
32
    public double start;
 
33
    public double end;
 
34
  }
 
35
  
 
36
  public static TimelineLogger? timeline_singleton;
 
37
  public static bool is_logging; 
 
38
  
 
39
  public class TimelineLogger : Object
 
40
  {
 
41
    private Timer global_timer;
 
42
    private Gee.HashMap<string, ProcessInfo> process_map;
 
43
    
 
44
    public static unowned Unity.TimelineLogger get_default ()
 
45
    {
 
46
      if (Unity.timeline_singleton == null)
 
47
      {
 
48
        Unity.timeline_singleton = new Unity.TimelineLogger ();
 
49
      }
 
50
      
 
51
      return Unity.timeline_singleton;
 
52
    }
 
53
    
 
54
    construct
 
55
    {
 
56
      this.process_map = new Gee.HashMap<string, ProcessInfo> ();
 
57
      this.global_timer = new Timer ();      
 
58
      this.global_timer.start ();
 
59
    }
 
60
    
 
61
    public void start_process (string name)
 
62
    {
 
63
      if (name in this.process_map.keys)
 
64
      {
 
65
        warning ("already started process: %s", name);
 
66
        return;
 
67
      }
 
68
      
 
69
      var info = new ProcessInfo (name);
 
70
      this.process_map[name] = info;
 
71
      info.start = this.global_timer.elapsed ();
 
72
    }
 
73
    
 
74
    public void end_process (string name)
 
75
    {
 
76
      double end_time = this.global_timer.elapsed ();
 
77
      
 
78
      if (name in this.process_map.keys)
 
79
      {
 
80
        this.process_map[name].end = end_time;
 
81
      }
 
82
      else 
 
83
      {
 
84
        warning ("process %s not started", name);
 
85
      }
 
86
    }
 
87
    
 
88
    public void write_log (string filename)
 
89
    {
 
90
      debug ("Writing performance log file: %s...", filename);
 
91
      var log_file = File.new_for_path (filename);
 
92
      FileOutputStream file_stream;
 
93
      try 
 
94
      {
 
95
        if (!log_file.query_exists (null)) {
 
96
          file_stream = log_file.create (FileCreateFlags.NONE, null);
 
97
        }
 
98
        else 
 
99
        {
 
100
          file_stream = log_file.replace (null, false, FileCreateFlags.NONE, null);
 
101
        }
 
102
        
 
103
        var output_stream = new DataOutputStream (file_stream);
 
104
        
 
105
        foreach (ProcessInfo info in this.process_map.values)
 
106
        {
 
107
          string outline = "%s, %f, %f\n".printf(info.name, info.start, info.end);
 
108
            output_stream.put_string (outline, null); 
 
109
        }
 
110
      } catch (Error e)
 
111
      {
 
112
        warning (e.message);
 
113
      }
 
114
    }
 
115
  }
 
116
}