暂无描述

encode.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. "errors"
  17. "fmt"
  18. "sort"
  19. )
  20. func (p *Profile) decoder() []decoder {
  21. return profileDecoder
  22. }
  23. // preEncode populates the unexported fields to be used by encode
  24. // (with suffix X) from the corresponding exported fields. The
  25. // exported fields are cleared up to facilitate testing.
  26. func (p *Profile) preEncode() {
  27. strings := make(map[string]int)
  28. addString(strings, "")
  29. for _, st := range p.SampleType {
  30. st.typeX = addString(strings, st.Type)
  31. st.unitX = addString(strings, st.Unit)
  32. }
  33. for _, s := range p.Sample {
  34. s.labelX = nil
  35. var keys []string
  36. for k := range s.Label {
  37. keys = append(keys, k)
  38. }
  39. sort.Strings(keys)
  40. for _, k := range keys {
  41. vs := s.Label[k]
  42. for _, v := range vs {
  43. s.labelX = append(s.labelX,
  44. label{
  45. keyX: addString(strings, k),
  46. strX: addString(strings, v),
  47. },
  48. )
  49. }
  50. }
  51. var numKeys []string
  52. for k := range s.NumLabel {
  53. numKeys = append(numKeys, k)
  54. }
  55. sort.Strings(numKeys)
  56. for _, k := range numKeys {
  57. vs := s.NumLabel[k]
  58. for _, v := range vs {
  59. s.labelX = append(s.labelX,
  60. label{
  61. keyX: addString(strings, k),
  62. numX: v,
  63. },
  64. )
  65. }
  66. }
  67. s.locationIDX = nil
  68. for _, l := range s.Location {
  69. s.locationIDX = append(s.locationIDX, l.ID)
  70. }
  71. }
  72. for _, m := range p.Mapping {
  73. m.fileX = addString(strings, m.File)
  74. m.buildIDX = addString(strings, m.BuildID)
  75. }
  76. for _, l := range p.Location {
  77. for i, ln := range l.Line {
  78. if ln.Function != nil {
  79. l.Line[i].functionIDX = ln.Function.ID
  80. } else {
  81. l.Line[i].functionIDX = 0
  82. }
  83. }
  84. if l.Mapping != nil {
  85. l.mappingIDX = l.Mapping.ID
  86. } else {
  87. l.mappingIDX = 0
  88. }
  89. }
  90. for _, f := range p.Function {
  91. f.nameX = addString(strings, f.Name)
  92. f.systemNameX = addString(strings, f.SystemName)
  93. f.filenameX = addString(strings, f.Filename)
  94. }
  95. p.dropFramesX = addString(strings, p.DropFrames)
  96. p.keepFramesX = addString(strings, p.KeepFrames)
  97. if pt := p.PeriodType; pt != nil {
  98. pt.typeX = addString(strings, pt.Type)
  99. pt.unitX = addString(strings, pt.Unit)
  100. }
  101. p.commentX = nil
  102. for _, c := range p.Comments {
  103. p.commentX = append(p.commentX, addString(strings, c))
  104. }
  105. p.stringTable = make([]string, len(strings))
  106. for s, i := range strings {
  107. p.stringTable[i] = s
  108. }
  109. }
  110. func (p *Profile) encode(b *buffer) {
  111. for _, x := range p.SampleType {
  112. encodeMessage(b, 1, x)
  113. }
  114. for _, x := range p.Sample {
  115. encodeMessage(b, 2, x)
  116. }
  117. for _, x := range p.Mapping {
  118. encodeMessage(b, 3, x)
  119. }
  120. for _, x := range p.Location {
  121. encodeMessage(b, 4, x)
  122. }
  123. for _, x := range p.Function {
  124. encodeMessage(b, 5, x)
  125. }
  126. encodeStrings(b, 6, p.stringTable)
  127. encodeInt64Opt(b, 7, p.dropFramesX)
  128. encodeInt64Opt(b, 8, p.keepFramesX)
  129. encodeInt64Opt(b, 9, p.TimeNanos)
  130. encodeInt64Opt(b, 10, p.DurationNanos)
  131. if pt := p.PeriodType; pt != nil && (pt.typeX != 0 || pt.unitX != 0) {
  132. encodeMessage(b, 11, p.PeriodType)
  133. }
  134. encodeInt64Opt(b, 12, p.Period)
  135. encodeInt64s(b, 13, p.commentX)
  136. }
  137. var profileDecoder = []decoder{
  138. nil, // 0
  139. // repeated ValueType sample_type = 1
  140. func(b *buffer, m message) error {
  141. x := new(ValueType)
  142. pp := m.(*Profile)
  143. pp.SampleType = append(pp.SampleType, x)
  144. return decodeMessage(b, x)
  145. },
  146. // repeated Sample sample = 2
  147. func(b *buffer, m message) error {
  148. x := new(Sample)
  149. pp := m.(*Profile)
  150. pp.Sample = append(pp.Sample, x)
  151. return decodeMessage(b, x)
  152. },
  153. // repeated Mapping mapping = 3
  154. func(b *buffer, m message) error {
  155. x := new(Mapping)
  156. pp := m.(*Profile)
  157. pp.Mapping = append(pp.Mapping, x)
  158. return decodeMessage(b, x)
  159. },
  160. // repeated Location location = 4
  161. func(b *buffer, m message) error {
  162. x := new(Location)
  163. pp := m.(*Profile)
  164. pp.Location = append(pp.Location, x)
  165. return decodeMessage(b, x)
  166. },
  167. // repeated Function function = 5
  168. func(b *buffer, m message) error {
  169. x := new(Function)
  170. pp := m.(*Profile)
  171. pp.Function = append(pp.Function, x)
  172. return decodeMessage(b, x)
  173. },
  174. // repeated string string_table = 6
  175. func(b *buffer, m message) error {
  176. err := decodeStrings(b, &m.(*Profile).stringTable)
  177. if err != nil {
  178. return err
  179. }
  180. if *&m.(*Profile).stringTable[0] != "" {
  181. return errors.New("string_table[0] must be ''")
  182. }
  183. return nil
  184. },
  185. // int64 drop_frames = 7
  186. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Profile).dropFramesX) },
  187. // int64 keep_frames = 8
  188. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Profile).keepFramesX) },
  189. // int64 time_nanos = 9
  190. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Profile).TimeNanos) },
  191. // int64 duration_nanos = 10
  192. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Profile).DurationNanos) },
  193. // ValueType period_type = 11
  194. func(b *buffer, m message) error {
  195. x := new(ValueType)
  196. pp := m.(*Profile)
  197. pp.PeriodType = x
  198. return decodeMessage(b, x)
  199. },
  200. // int64 period = 12
  201. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Profile).Period) },
  202. // repeated int64 comment = 13
  203. func(b *buffer, m message) error { return decodeInt64s(b, &m.(*Profile).commentX) },
  204. }
  205. // postDecode takes the unexported fields populated by decode (with
  206. // suffix X) and populates the corresponding exported fields.
  207. // The unexported fields are cleared up to facilitate testing.
  208. func (p *Profile) postDecode() error {
  209. var err error
  210. mappings := make(map[uint64]*Mapping)
  211. for _, m := range p.Mapping {
  212. m.File, err = getString(p.stringTable, &m.fileX, err)
  213. m.BuildID, err = getString(p.stringTable, &m.buildIDX, err)
  214. mappings[m.ID] = m
  215. }
  216. functions := make(map[uint64]*Function)
  217. for _, f := range p.Function {
  218. f.Name, err = getString(p.stringTable, &f.nameX, err)
  219. f.SystemName, err = getString(p.stringTable, &f.systemNameX, err)
  220. f.Filename, err = getString(p.stringTable, &f.filenameX, err)
  221. functions[f.ID] = f
  222. }
  223. locations := make(map[uint64]*Location)
  224. for _, l := range p.Location {
  225. l.Mapping = mappings[l.mappingIDX]
  226. l.mappingIDX = 0
  227. for i, ln := range l.Line {
  228. if id := ln.functionIDX; id != 0 {
  229. l.Line[i].Function = functions[id]
  230. if l.Line[i].Function == nil {
  231. return fmt.Errorf("Function ID %d not found", id)
  232. }
  233. l.Line[i].functionIDX = 0
  234. }
  235. }
  236. locations[l.ID] = l
  237. }
  238. for _, st := range p.SampleType {
  239. st.Type, err = getString(p.stringTable, &st.typeX, err)
  240. st.Unit, err = getString(p.stringTable, &st.unitX, err)
  241. }
  242. for _, s := range p.Sample {
  243. labels := make(map[string][]string)
  244. numLabels := make(map[string][]int64)
  245. for _, l := range s.labelX {
  246. var key, value string
  247. key, err = getString(p.stringTable, &l.keyX, err)
  248. if l.strX != 0 {
  249. value, err = getString(p.stringTable, &l.strX, err)
  250. labels[key] = append(labels[key], value)
  251. } else if l.numX != 0 {
  252. numLabels[key] = append(numLabels[key], l.numX)
  253. }
  254. }
  255. if len(labels) > 0 {
  256. s.Label = labels
  257. }
  258. if len(numLabels) > 0 {
  259. s.NumLabel = numLabels
  260. }
  261. s.Location = nil
  262. for _, lid := range s.locationIDX {
  263. s.Location = append(s.Location, locations[lid])
  264. }
  265. s.locationIDX = nil
  266. }
  267. p.DropFrames, err = getString(p.stringTable, &p.dropFramesX, err)
  268. p.KeepFrames, err = getString(p.stringTable, &p.keepFramesX, err)
  269. if pt := p.PeriodType; pt == nil {
  270. p.PeriodType = &ValueType{}
  271. }
  272. if pt := p.PeriodType; pt != nil {
  273. pt.Type, err = getString(p.stringTable, &pt.typeX, err)
  274. pt.Unit, err = getString(p.stringTable, &pt.unitX, err)
  275. }
  276. for _, i := range p.commentX {
  277. var c string
  278. c, err = getString(p.stringTable, &i, err)
  279. p.Comments = append(p.Comments, c)
  280. }
  281. p.commentX = nil
  282. p.stringTable = nil
  283. return err
  284. }
  285. func (p *ValueType) decoder() []decoder {
  286. return valueTypeDecoder
  287. }
  288. func (p *ValueType) encode(b *buffer) {
  289. encodeInt64Opt(b, 1, p.typeX)
  290. encodeInt64Opt(b, 2, p.unitX)
  291. }
  292. var valueTypeDecoder = []decoder{
  293. nil, // 0
  294. // optional int64 type = 1
  295. func(b *buffer, m message) error { return decodeInt64(b, &m.(*ValueType).typeX) },
  296. // optional int64 unit = 2
  297. func(b *buffer, m message) error { return decodeInt64(b, &m.(*ValueType).unitX) },
  298. }
  299. func (p *Sample) decoder() []decoder {
  300. return sampleDecoder
  301. }
  302. func (p *Sample) encode(b *buffer) {
  303. encodeUint64s(b, 1, p.locationIDX)
  304. encodeInt64s(b, 2, p.Value)
  305. for _, x := range p.labelX {
  306. encodeMessage(b, 3, x)
  307. }
  308. }
  309. var sampleDecoder = []decoder{
  310. nil, // 0
  311. // repeated uint64 location = 1
  312. func(b *buffer, m message) error { return decodeUint64s(b, &m.(*Sample).locationIDX) },
  313. // repeated int64 value = 2
  314. func(b *buffer, m message) error { return decodeInt64s(b, &m.(*Sample).Value) },
  315. // repeated Label label = 3
  316. func(b *buffer, m message) error {
  317. s := m.(*Sample)
  318. n := len(s.labelX)
  319. s.labelX = append(s.labelX, label{})
  320. return decodeMessage(b, &s.labelX[n])
  321. },
  322. }
  323. func (p label) decoder() []decoder {
  324. return labelDecoder
  325. }
  326. func (p label) encode(b *buffer) {
  327. encodeInt64Opt(b, 1, p.keyX)
  328. encodeInt64Opt(b, 2, p.strX)
  329. encodeInt64Opt(b, 3, p.numX)
  330. }
  331. var labelDecoder = []decoder{
  332. nil, // 0
  333. // optional int64 key = 1
  334. func(b *buffer, m message) error { return decodeInt64(b, &m.(*label).keyX) },
  335. // optional int64 str = 2
  336. func(b *buffer, m message) error { return decodeInt64(b, &m.(*label).strX) },
  337. // optional int64 num = 3
  338. func(b *buffer, m message) error { return decodeInt64(b, &m.(*label).numX) },
  339. }
  340. func (p *Mapping) decoder() []decoder {
  341. return mappingDecoder
  342. }
  343. func (p *Mapping) encode(b *buffer) {
  344. encodeUint64Opt(b, 1, p.ID)
  345. encodeUint64Opt(b, 2, p.Start)
  346. encodeUint64Opt(b, 3, p.Limit)
  347. encodeUint64Opt(b, 4, p.Offset)
  348. encodeInt64Opt(b, 5, p.fileX)
  349. encodeInt64Opt(b, 6, p.buildIDX)
  350. encodeBoolOpt(b, 7, p.HasFunctions)
  351. encodeBoolOpt(b, 8, p.HasFilenames)
  352. encodeBoolOpt(b, 9, p.HasLineNumbers)
  353. encodeBoolOpt(b, 10, p.HasInlineFrames)
  354. }
  355. var mappingDecoder = []decoder{
  356. nil, // 0
  357. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Mapping).ID) }, // optional uint64 id = 1
  358. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Mapping).Start) }, // optional uint64 memory_offset = 2
  359. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Mapping).Limit) }, // optional uint64 memory_limit = 3
  360. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Mapping).Offset) }, // optional uint64 file_offset = 4
  361. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Mapping).fileX) }, // optional int64 filename = 5
  362. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Mapping).buildIDX) }, // optional int64 build_id = 6
  363. func(b *buffer, m message) error { return decodeBool(b, &m.(*Mapping).HasFunctions) }, // optional bool has_functions = 7
  364. func(b *buffer, m message) error { return decodeBool(b, &m.(*Mapping).HasFilenames) }, // optional bool has_filenames = 8
  365. func(b *buffer, m message) error { return decodeBool(b, &m.(*Mapping).HasLineNumbers) }, // optional bool has_line_numbers = 9
  366. func(b *buffer, m message) error { return decodeBool(b, &m.(*Mapping).HasInlineFrames) }, // optional bool has_inline_frames = 10
  367. }
  368. func (p *Location) decoder() []decoder {
  369. return locationDecoder
  370. }
  371. func (p *Location) encode(b *buffer) {
  372. encodeUint64Opt(b, 1, p.ID)
  373. encodeUint64Opt(b, 2, p.mappingIDX)
  374. encodeUint64Opt(b, 3, p.Address)
  375. for i := range p.Line {
  376. encodeMessage(b, 4, &p.Line[i])
  377. }
  378. }
  379. var locationDecoder = []decoder{
  380. nil, // 0
  381. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Location).ID) }, // optional uint64 id = 1;
  382. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Location).mappingIDX) }, // optional uint64 mapping_id = 2;
  383. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Location).Address) }, // optional uint64 address = 3;
  384. func(b *buffer, m message) error { // repeated Line line = 4
  385. pp := m.(*Location)
  386. n := len(pp.Line)
  387. pp.Line = append(pp.Line, Line{})
  388. return decodeMessage(b, &pp.Line[n])
  389. },
  390. }
  391. func (p *Line) decoder() []decoder {
  392. return lineDecoder
  393. }
  394. func (p *Line) encode(b *buffer) {
  395. encodeUint64Opt(b, 1, p.functionIDX)
  396. encodeInt64Opt(b, 2, p.Line)
  397. }
  398. var lineDecoder = []decoder{
  399. nil, // 0
  400. // optional uint64 function_id = 1
  401. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Line).functionIDX) },
  402. // optional int64 line = 2
  403. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Line).Line) },
  404. }
  405. func (p *Function) decoder() []decoder {
  406. return functionDecoder
  407. }
  408. func (p *Function) encode(b *buffer) {
  409. encodeUint64Opt(b, 1, p.ID)
  410. encodeInt64Opt(b, 2, p.nameX)
  411. encodeInt64Opt(b, 3, p.systemNameX)
  412. encodeInt64Opt(b, 4, p.filenameX)
  413. encodeInt64Opt(b, 5, p.StartLine)
  414. }
  415. var functionDecoder = []decoder{
  416. nil, // 0
  417. // optional uint64 id = 1
  418. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Function).ID) },
  419. // optional int64 function_name = 2
  420. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Function).nameX) },
  421. // optional int64 function_system_name = 3
  422. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Function).systemNameX) },
  423. // repeated int64 filename = 4
  424. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Function).filenameX) },
  425. // optional int64 start_line = 5
  426. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Function).StartLine) },
  427. }
  428. func addString(strings map[string]int, s string) int64 {
  429. i, ok := strings[s]
  430. if !ok {
  431. i = len(strings)
  432. strings[s] = i
  433. }
  434. return int64(i)
  435. }
  436. func getString(strings []string, strng *int64, err error) (string, error) {
  437. if err != nil {
  438. return "", err
  439. }
  440. s := int(*strng)
  441. if s < 0 || s >= len(strings) {
  442. return "", errMalformed
  443. }
  444. *strng = 0
  445. return strings[s], nil
  446. }