• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2019 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18	"flag"
19	"log"
20	"os"
21
22	"github.com/google/pprof/profile"
23)
24
25var (
26	output string
27)
28
29func main() {
30	flag.StringVar(&output, "o", "merged.data", "")
31	flag.Parse()
32
33	files := os.Args[1:]
34	if len(files) == 0 {
35		log.Fatal("Give profiles files as arguments")
36	}
37
38	var profiles []*profile.Profile
39	for _, fname := range files {
40		f, err := os.Open(fname)
41		if err != nil {
42			log.Fatalf("Cannot open profile file at %q: %v", fname, err)
43		}
44		p, err := profile.Parse(f)
45		if err != nil {
46			log.Fatalf("Cannot parse profile at %q: %v", fname, err)
47		}
48		profiles = append(profiles, p)
49	}
50
51	merged, err := profile.Merge(profiles)
52	if err != nil {
53		log.Fatalf("Cannot merge profiles: %v", err)
54	}
55
56	out, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE, 0755)
57	if err != nil {
58		log.Fatalf("Cannot open output to write: %v", err)
59	}
60
61	if err := merged.Write(out); err != nil {
62		log.Fatalf("Cannot write merged profile to file: %v", err)
63	}
64
65	if err := out.Close(); err != nil {
66		log.Printf("Error when closing the output file: %v", err)
67	}
68}
69