config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2020 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. // Configuration for perf event manager.
  15. package perf
  16. import (
  17. "encoding/json"
  18. "fmt"
  19. "os"
  20. "strconv"
  21. "k8s.io/klog/v2"
  22. )
  23. type PerfEvents struct {
  24. // Core perf events to be measured.
  25. Core Events `json:"core,omitempty"`
  26. // Uncore perf events to be measured.
  27. Uncore Events `json:"uncore,omitempty"`
  28. }
  29. type Events struct {
  30. // List of perf events' names to be measured.
  31. Events []Group `json:"events"`
  32. // List of custom perf events' to be measured. It is impossible to
  33. // specify some events using their names and in such case you have
  34. // to provide lower level configuration.
  35. CustomEvents []CustomEvent `json:"custom_events"`
  36. }
  37. type Event string
  38. type CustomEvent struct {
  39. // Type of the event. See perf_event_attr documentation
  40. // at man perf_event_open.
  41. Type uint32 `json:"type,omitempty"`
  42. // Symbolically formed event like:
  43. // pmu/config=PerfEvent.Config[0],config1=PerfEvent.Config[1],config2=PerfEvent.Config[2]
  44. // as described in man perf-stat.
  45. Config Config `json:"config"`
  46. // Human readable name of metric that will be created from the event.
  47. Name Event `json:"name"`
  48. }
  49. type Config []uint64
  50. func (c *Config) UnmarshalJSON(b []byte) error {
  51. config := []string{}
  52. err := json.Unmarshal(b, &config)
  53. if err != nil {
  54. klog.Errorf("Unmarshalling %s into slice of strings failed: %q", b, err)
  55. return fmt.Errorf("unmarshalling %s into slice of strings failed: %q", b, err)
  56. }
  57. intermediate := []uint64{}
  58. for _, v := range config {
  59. uintValue, err := strconv.ParseUint(v, 0, 64)
  60. if err != nil {
  61. klog.Errorf("Parsing %#v into uint64 failed: %q", v, err)
  62. return fmt.Errorf("parsing %#v into uint64 failed: %q", v, err)
  63. }
  64. intermediate = append(intermediate, uintValue)
  65. }
  66. *c = intermediate
  67. return nil
  68. }
  69. func parseConfig(file *os.File) (events PerfEvents, err error) {
  70. decoder := json.NewDecoder(file)
  71. err = decoder.Decode(&events)
  72. if err != nil {
  73. err = fmt.Errorf("unable to load perf events configuration from %q: %q", file.Name(), err)
  74. return
  75. }
  76. return
  77. }
  78. type Group struct {
  79. events []Event
  80. array bool
  81. }
  82. func (g *Group) UnmarshalJSON(b []byte) error {
  83. var jsonObj interface{}
  84. err := json.Unmarshal(b, &jsonObj)
  85. if err != nil {
  86. return err
  87. }
  88. switch obj := jsonObj.(type) {
  89. case string:
  90. *g = Group{
  91. events: []Event{Event(obj)},
  92. array: false,
  93. }
  94. return nil
  95. case []interface{}:
  96. group := Group{
  97. events: make([]Event, 0, len(obj)),
  98. array: true,
  99. }
  100. for _, v := range obj {
  101. value, ok := v.(string)
  102. if !ok {
  103. return fmt.Errorf("cannot unmarshal %v", value)
  104. }
  105. group.events = append(group.events, Event(value))
  106. }
  107. *g = group
  108. return nil
  109. }
  110. return fmt.Errorf("unsupported type")
  111. }