gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/profiling/cmd/flags.go (about)

     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  
    19  package main
    20  
    21  import (
    22  	"flag"
    23  	"fmt"
    24  )
    25  
    26  var flagAddress = flag.String("address", "", "address of a remote gRPC server with profiling turned on to retrieve stats from")
    27  var flagTimeout = flag.Int("timeout", 0, "network operations timeout in seconds to remote target (0 indicates unlimited)")
    28  
    29  var flagRetrieveSnapshot = flag.Bool("retrieve-snapshot", false, "connect to remote target and retrieve a profiling snapshot locally for processing")
    30  var flagSnapshot = flag.String("snapshot", "", "snapshot file to write to when retrieving profiling data or snapshot file to read from when processing profiling data")
    31  
    32  var flagEnableProfiling = flag.Bool("enable-profiling", false, "enable profiling in remote target")
    33  var flagDisableProfiling = flag.Bool("disable-profiling", false, "disable profiling in remote target")
    34  
    35  var flagStreamStatsCatapultJSON = flag.String("stream-stats-catapult-json", "", "path to a file to write to after transforming a snapshot into catapult's JSON format")
    36  var flagStreamStatsFilter = flag.String("stream-stats-filter", "server,client", "comma-separated list of stat tags to filter for")
    37  
    38  func exactlyOneOf(opts ...bool) bool {
    39  	first := true
    40  	for _, o := range opts {
    41  		if !o {
    42  			continue
    43  		}
    44  
    45  		if first {
    46  			first = false
    47  		} else {
    48  			return false
    49  		}
    50  	}
    51  
    52  	return !first
    53  }
    54  
    55  func parseArgs() error {
    56  	flag.Parse()
    57  
    58  	if *flagAddress != "" {
    59  		if !exactlyOneOf(*flagEnableProfiling, *flagDisableProfiling, *flagRetrieveSnapshot) {
    60  			return fmt.Errorf("when -address is specified, you must include exactly only one of -enable-profiling, -disable-profiling, and -retrieve-snapshot")
    61  		}
    62  
    63  		if *flagStreamStatsCatapultJSON != "" {
    64  			return fmt.Errorf("when -address is specified, you must not include -stream-stats-catapult-json")
    65  		}
    66  	} else {
    67  		if *flagEnableProfiling || *flagDisableProfiling || *flagRetrieveSnapshot {
    68  			return fmt.Errorf("when -address isn't specified, you must not include any of -enable-profiling, -disable-profiling, and -retrieve-snapshot")
    69  		}
    70  
    71  		if *flagStreamStatsCatapultJSON == "" {
    72  			return fmt.Errorf("when -address isn't specified, you must include -stream-stats-catapult-json")
    73  		}
    74  	}
    75  
    76  	return nil
    77  }