~unity-team/unity/trusty-1374785

« back to all changes in this revision

Viewing changes to libunity/perf-logger.vala

  • Committer: Gordon Allott
  • Date: 2009-12-16 13:25:32 UTC
  • mto: This revision was merged to the branch mainline in revision 45.
  • Revision ID: mail@gordallott.com-20091216132532-3zp0dlxqkqjnsmwf
performance logger support and boot chart generation script

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 TimelineLogger? timeline_singleton;
 
37
  
 
38
  public class TimelineLogger : Object
 
39
  {
 
40
    private Timer global_timer;
 
41
    private Gee.HashMap<string, ProcessInfo> process_map;
 
42
    
 
43
    public static unowned Unity.TimelineLogger get_default ()
 
44
    {
 
45
      if (Unity.timeline_singleton == null)
 
46
      {
 
47
        Unity.timeline_singleton = new Unity.TimelineLogger ();
 
48
      }
 
49
      
 
50
      return Unity.timeline_singleton;
 
51
    }
 
52
    
 
53
    construct
 
54
    {
 
55
      this.process_map = new Gee.HashMap<string, ProcessInfo> ();
 
56
      this.global_timer = new Timer ();      
 
57
      this.global_timer.start ();
 
58
    }
 
59
    
 
60
    public void start_process (string name)
 
61
    {
 
62
      if (name in this.process_map.keys)
 
63
      {
 
64
        warning ("already started process: %s", name);
 
65
        return;
 
66
      }
 
67
      
 
68
      var info = new ProcessInfo (name);
 
69
      this.process_map[name] = info;
 
70
      info.start = this.global_timer.elapsed ();
 
71
    }
 
72
    
 
73
    public void end_process (string name)
 
74
    {
 
75
      double end_time = this.global_timer.elapsed ();
 
76
      
 
77
      if (name in this.process_map.keys)
 
78
      {
 
79
        this.process_map[name].end = end_time;
 
80
      }
 
81
      else 
 
82
      {
 
83
        warning ("process %s not started", name);
 
84
      }
 
85
    }
 
86
    
 
87
    public void write_log (string filename)
 
88
    {
 
89
      debug ("Writing performance log file: %s...", filename);
 
90
      var log_file = File.new_for_path (filename);
 
91
      FileOutputStream file_stream;
 
92
      try 
 
93
      {
 
94
        if (!log_file.query_exists (null)) {
 
95
          file_stream = log_file.create (FileCreateFlags.NONE, null);
 
96
        }
 
97
        else 
 
98
        {
 
99
          file_stream = log_file.replace (null, false, FileCreateFlags.NONE, null);
 
100
        }
 
101
        
 
102
        var output_stream = new DataOutputStream (file_stream);
 
103
        
 
104
        foreach (ProcessInfo info in this.process_map.values)
 
105
        {
 
106
          string outline = "%s, %f, %f\n".printf(info.name, info.start, info.end);
 
107
            output_stream.put_string (outline, null); 
 
108
        }
 
109
      } catch (Error e)
 
110
      {
 
111
        warning (e.message);
 
112
      }
 
113
    }
 
114
  }
 
115
}