暫無描述

merge.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // Copyright 2014 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. package profile
  15. import (
  16. "fmt"
  17. "reflect"
  18. "sort"
  19. "strconv"
  20. "strings"
  21. )
  22. // Compact performs garbage collection on a profile to remove any
  23. // unreferenced fields. This is useful to reduce the size of a profile
  24. // after samples or locations have been removed.
  25. func (p *Profile) Compact() *Profile {
  26. p, _ = Merge([]*Profile{p})
  27. return p
  28. }
  29. // Merge merges all the profiles in profs into a single Profile.
  30. // Returns a new profile independent of the input profiles. The merged
  31. // profile is compacted to eliminate unused samples, locations,
  32. // functions and mappings. Profiles must have identical profile sample
  33. // and period types or the merge will fail. profile.Period of the
  34. // resulting profile will be the maximum of all profiles, and
  35. // profile.TimeNanos will be the earliest nonzero one.
  36. func Merge(srcs []*Profile) (*Profile, error) {
  37. if len(srcs) == 0 {
  38. return nil, fmt.Errorf("no profiles to merge")
  39. }
  40. p, err := combineHeaders(srcs)
  41. if err != nil {
  42. return nil, err
  43. }
  44. pm := &profileMerger{
  45. p: p,
  46. samples: make(map[sampleKey]*Sample, len(srcs[0].Sample)),
  47. locations: make(map[locationKey]*Location, len(srcs[0].Location)),
  48. functions: make(map[functionKey]*Function, len(srcs[0].Function)),
  49. mappings: make(map[mappingKey]*Mapping, len(srcs[0].Mapping)),
  50. }
  51. for _, src := range srcs {
  52. // Clear the profile-specific hash tables
  53. pm.locationsByID = make(map[uint64]*Location, len(src.Location))
  54. pm.functionsByID = make(map[uint64]*Function, len(src.Function))
  55. pm.mappingsByID = make(map[uint64]mapInfo, len(src.Mapping))
  56. if len(pm.mappings) == 0 && len(src.Mapping) > 0 {
  57. // The Mapping list has the property that the first mapping
  58. // represents the main binary. Take the first Mapping we see,
  59. // otherwise the operations below will add mappings in an
  60. // arbitrary order.
  61. pm.mapMapping(srcs[0].Mapping[0])
  62. }
  63. for _, s := range src.Sample {
  64. if !isZeroSample(s) {
  65. pm.mapSample(s)
  66. }
  67. }
  68. }
  69. for _, s := range p.Sample {
  70. if isZeroSample(s) {
  71. // If there are any zero samples, re-merge the profile to GC
  72. // them.
  73. return Merge([]*Profile{p})
  74. }
  75. }
  76. return p, nil
  77. }
  78. func isZeroSample(s *Sample) bool {
  79. for _, v := range s.Value {
  80. if v != 0 {
  81. return false
  82. }
  83. }
  84. return true
  85. }
  86. type profileMerger struct {
  87. p *Profile
  88. // Memoization tables within a profile.
  89. locationsByID map[uint64]*Location
  90. functionsByID map[uint64]*Function
  91. mappingsByID map[uint64]mapInfo
  92. // Memoization tables for profile entities.
  93. samples map[sampleKey]*Sample
  94. locations map[locationKey]*Location
  95. functions map[functionKey]*Function
  96. mappings map[mappingKey]*Mapping
  97. }
  98. type mapInfo struct {
  99. m *Mapping
  100. offset int64
  101. }
  102. func (pm *profileMerger) mapSample(src *Sample) *Sample {
  103. s := &Sample{
  104. Location: make([]*Location, len(src.Location)),
  105. Value: make([]int64, len(src.Value)),
  106. Label: make(map[string][]string, len(src.Label)),
  107. NumLabel: make(map[string][]int64, len(src.NumLabel)),
  108. }
  109. for i, l := range src.Location {
  110. s.Location[i] = pm.mapLocation(l)
  111. }
  112. for k, v := range src.Label {
  113. vv := make([]string, len(v))
  114. copy(vv, v)
  115. s.Label[k] = vv
  116. }
  117. for k, v := range src.NumLabel {
  118. vv := make([]int64, len(v))
  119. copy(vv, v)
  120. s.NumLabel[k] = vv
  121. }
  122. // Check memoization table. Must be done on the remapped location to
  123. // account for the remapped mapping. Add current values to the
  124. // existing sample.
  125. k := s.key()
  126. if ss, ok := pm.samples[k]; ok {
  127. for i, v := range src.Value {
  128. ss.Value[i] += v
  129. }
  130. return ss
  131. }
  132. copy(s.Value, src.Value)
  133. pm.samples[k] = s
  134. pm.p.Sample = append(pm.p.Sample, s)
  135. return s
  136. }
  137. // key generates sampleKey to be used as a key for maps.
  138. func (sample *Sample) key() sampleKey {
  139. ids := make([]string, len(sample.Location))
  140. for i, l := range sample.Location {
  141. ids[i] = strconv.FormatUint(l.ID, 16)
  142. }
  143. labels := make([]string, 0, len(sample.Label))
  144. for k, v := range sample.Label {
  145. labels = append(labels, fmt.Sprintf("%q%q", k, v))
  146. }
  147. sort.Strings(labels)
  148. numlabels := make([]string, 0, len(sample.NumLabel))
  149. for k, v := range sample.NumLabel {
  150. numlabels = append(numlabels, fmt.Sprintf("%q%x", k, v))
  151. }
  152. sort.Strings(numlabels)
  153. return sampleKey{
  154. strings.Join(ids, "|"),
  155. strings.Join(labels, ""),
  156. strings.Join(numlabels, ""),
  157. }
  158. }
  159. type sampleKey struct {
  160. locations string
  161. labels string
  162. numlabels string
  163. }
  164. func (pm *profileMerger) mapLocation(src *Location) *Location {
  165. if src == nil {
  166. return nil
  167. }
  168. if l, ok := pm.locationsByID[src.ID]; ok {
  169. pm.locationsByID[src.ID] = l
  170. return l
  171. }
  172. mi := pm.mapMapping(src.Mapping)
  173. l := &Location{
  174. ID: uint64(len(pm.p.Location) + 1),
  175. Mapping: mi.m,
  176. Address: uint64(int64(src.Address) + mi.offset),
  177. Line: make([]Line, len(src.Line)),
  178. }
  179. for i, ln := range src.Line {
  180. l.Line[i] = pm.mapLine(ln)
  181. }
  182. // Check memoization table. Must be done on the remapped location to
  183. // account for the remapped mapping ID.
  184. k := l.key()
  185. if ll, ok := pm.locations[k]; ok {
  186. pm.locationsByID[src.ID] = ll
  187. return ll
  188. }
  189. pm.locationsByID[src.ID] = l
  190. pm.locations[k] = l
  191. pm.p.Location = append(pm.p.Location, l)
  192. return l
  193. }
  194. // key generates locationKey to be used as a key for maps.
  195. func (l *Location) key() locationKey {
  196. key := locationKey{
  197. addr: l.Address,
  198. }
  199. if l.Mapping != nil {
  200. // Normalizes address to handle address space randomization.
  201. key.addr -= l.Mapping.Start
  202. key.mappingID = l.Mapping.ID
  203. }
  204. lines := make([]string, len(l.Line)*2)
  205. for i, line := range l.Line {
  206. if line.Function != nil {
  207. lines[i*2] = strconv.FormatUint(line.Function.ID, 16)
  208. }
  209. lines[i*2+1] = strconv.FormatInt(line.Line, 16)
  210. }
  211. key.lines = strings.Join(lines, "|")
  212. return key
  213. }
  214. type locationKey struct {
  215. addr, mappingID uint64
  216. lines string
  217. }
  218. func (pm *profileMerger) mapMapping(src *Mapping) mapInfo {
  219. if src == nil {
  220. return mapInfo{}
  221. }
  222. if mi, ok := pm.mappingsByID[src.ID]; ok {
  223. return mi
  224. }
  225. // Check memoization tables.
  226. bk, pk := src.key()
  227. if src.BuildID != "" {
  228. if m, ok := pm.mappings[bk]; ok {
  229. mi := mapInfo{m, int64(m.Start) - int64(src.Start)}
  230. pm.mappingsByID[src.ID] = mi
  231. return mi
  232. }
  233. }
  234. if src.File != "" {
  235. if m, ok := pm.mappings[pk]; ok {
  236. mi := mapInfo{m, int64(m.Start) - int64(src.Start)}
  237. pm.mappingsByID[src.ID] = mi
  238. return mi
  239. }
  240. }
  241. m := &Mapping{
  242. ID: uint64(len(pm.p.Mapping) + 1),
  243. Start: src.Start,
  244. Limit: src.Limit,
  245. Offset: src.Offset,
  246. File: src.File,
  247. BuildID: src.BuildID,
  248. HasFunctions: src.HasFunctions,
  249. HasFilenames: src.HasFilenames,
  250. HasLineNumbers: src.HasLineNumbers,
  251. HasInlineFrames: src.HasInlineFrames,
  252. }
  253. pm.p.Mapping = append(pm.p.Mapping, m)
  254. // Update memoization tables.
  255. if m.BuildID != "" {
  256. pm.mappings[bk] = m
  257. }
  258. if m.File != "" {
  259. pm.mappings[pk] = m
  260. }
  261. mi := mapInfo{m, 0}
  262. pm.mappingsByID[src.ID] = mi
  263. return mi
  264. }
  265. // key generates encoded strings of Mapping to be used as a key for
  266. // maps. The first key represents only the build id, while the second
  267. // represents only the file path.
  268. func (m *Mapping) key() (buildIDKey, pathKey mappingKey) {
  269. // Normalize addresses to handle address space randomization.
  270. // Round up to next 4K boundary to avoid minor discrepancies.
  271. const mapsizeRounding = 0x1000
  272. size := m.Limit - m.Start
  273. size = size + mapsizeRounding - 1
  274. size = size - (size % mapsizeRounding)
  275. buildIDKey = mappingKey{
  276. size,
  277. m.Offset,
  278. m.BuildID,
  279. }
  280. pathKey = mappingKey{
  281. size,
  282. m.Offset,
  283. m.File,
  284. }
  285. return
  286. }
  287. type mappingKey struct {
  288. size, offset uint64
  289. buildidIDOrFile string
  290. }
  291. func (pm *profileMerger) mapLine(src Line) Line {
  292. ln := Line{
  293. Function: pm.mapFunction(src.Function),
  294. Line: src.Line,
  295. }
  296. return ln
  297. }
  298. func (pm *profileMerger) mapFunction(src *Function) *Function {
  299. if src == nil {
  300. return nil
  301. }
  302. if f, ok := pm.functionsByID[src.ID]; ok {
  303. return f
  304. }
  305. k := src.key()
  306. if f, ok := pm.functions[k]; ok {
  307. pm.functionsByID[src.ID] = f
  308. return f
  309. }
  310. f := &Function{
  311. ID: uint64(len(pm.p.Function) + 1),
  312. Name: src.Name,
  313. SystemName: src.SystemName,
  314. Filename: src.Filename,
  315. StartLine: src.StartLine,
  316. }
  317. pm.functions[k] = f
  318. pm.functionsByID[src.ID] = f
  319. pm.p.Function = append(pm.p.Function, f)
  320. return f
  321. }
  322. // key generates a struct to be used as a key for maps.
  323. func (f *Function) key() functionKey {
  324. return functionKey{
  325. f.StartLine,
  326. f.Name,
  327. f.SystemName,
  328. f.Filename,
  329. }
  330. }
  331. type functionKey struct {
  332. startLine int64
  333. name, systemName, fileName string
  334. }
  335. // combineHeaders checks that all profiles can be merged and returns
  336. // their combined profile.
  337. func combineHeaders(srcs []*Profile) (*Profile, error) {
  338. for _, s := range srcs[1:] {
  339. if err := srcs[0].compatible(s); err != nil {
  340. return nil, err
  341. }
  342. }
  343. var timeNanos, durationNanos, period int64
  344. var comments []string
  345. var defaultSampleType string
  346. for _, s := range srcs {
  347. if timeNanos == 0 || s.TimeNanos < timeNanos {
  348. timeNanos = s.TimeNanos
  349. }
  350. durationNanos += s.DurationNanos
  351. if period == 0 || period < s.Period {
  352. period = s.Period
  353. }
  354. comments = append(comments, s.Comments...)
  355. if defaultSampleType == "" {
  356. defaultSampleType = s.DefaultSampleType
  357. }
  358. }
  359. p := &Profile{
  360. SampleType: make([]*ValueType, len(srcs[0].SampleType)),
  361. DropFrames: srcs[0].DropFrames,
  362. KeepFrames: srcs[0].KeepFrames,
  363. TimeNanos: timeNanos,
  364. DurationNanos: durationNanos,
  365. PeriodType: srcs[0].PeriodType,
  366. Period: period,
  367. Comments: comments,
  368. DefaultSampleType: defaultSampleType,
  369. }
  370. copy(p.SampleType, srcs[0].SampleType)
  371. return p, nil
  372. }
  373. // compatible determines if two profiles can be compared/merged.
  374. // returns nil if the profiles are compatible; otherwise an error with
  375. // details on the incompatibility.
  376. func (p *Profile) compatible(pb *Profile) error {
  377. if !reflect.DeepEqual(p.PeriodType, pb.PeriodType) {
  378. return fmt.Errorf("incompatible period types %v and %v", p.PeriodType, pb.PeriodType)
  379. }
  380. if len(p.SampleType) != len(pb.SampleType) {
  381. return fmt.Errorf("incompatible sample types %v and %v", p.SampleType, pb.SampleType)
  382. }
  383. for i := range p.SampleType {
  384. if !reflect.DeepEqual(p.SampleType[i], pb.SampleType[i]) {
  385. return fmt.Errorf("incompatible sample types %v and %v", p.SampleType, pb.SampleType)
  386. }
  387. }
  388. return nil
  389. }