flags.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. *
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package main
  19. import (
  20. "flag"
  21. "fmt"
  22. )
  23. var flagAddress = flag.String("address", "", "address of a remote gRPC server with profiling turned on to retrieve stats from")
  24. var flagTimeout = flag.Int("timeout", 0, "network operations timeout in seconds to remote target (0 indicates unlimited)")
  25. var flagRetrieveSnapshot = flag.Bool("retrieve-snapshot", false, "connect to remote target and retrieve a profiling snapshot locally for processing")
  26. var flagSnapshot = flag.String("snapshot", "", "snapshot file to write to when retrieving profiling data or snapshot file to read from when processing profiling data")
  27. var flagEnableProfiling = flag.Bool("enable-profiling", false, "enable profiling in remote target")
  28. var flagDisableProfiling = flag.Bool("disable-profiling", false, "disable profiling in remote target")
  29. var flagStreamStatsCatapultJSON = flag.String("stream-stats-catapult-json", "", "path to a file to write to after transforming a snapshot into catapult's JSON format")
  30. var flagStreamStatsFilter = flag.String("stream-stats-filter", "server,client", "comma-separated list of stat tags to filter for")
  31. func exactlyOneOf(opts ...bool) bool {
  32. first := true
  33. for _, o := range opts {
  34. if !o {
  35. continue
  36. }
  37. if first {
  38. first = false
  39. } else {
  40. return false
  41. }
  42. }
  43. return !first
  44. }
  45. func parseArgs() error {
  46. flag.Parse()
  47. if *flagAddress != "" {
  48. if !exactlyOneOf(*flagEnableProfiling, *flagDisableProfiling, *flagRetrieveSnapshot) {
  49. return fmt.Errorf("when -address is specified, you must include exactly only one of -enable-profiling, -disable-profiling, and -retrieve-snapshot")
  50. }
  51. if *flagStreamStatsCatapultJSON != "" {
  52. return fmt.Errorf("when -address is specified, you must not include -stream-stats-catapult-json")
  53. }
  54. } else {
  55. if *flagEnableProfiling || *flagDisableProfiling || *flagRetrieveSnapshot {
  56. return fmt.Errorf("when -address isn't specified, you must not include any of -enable-profiling, -disable-profiling, and -retrieve-snapshot")
  57. }
  58. if *flagStreamStatsCatapultJSON == "" {
  59. return fmt.Errorf("when -address isn't specified, you must include -stream-stats-catapult-json")
  60. }
  61. }
  62. return nil
  63. }