...

Source file src/github.com/chaos-mesh/chaos-mesh/cmd/watchmaker/main_linux.go

Documentation: github.com/chaos-mesh/chaos-mesh/cmd/watchmaker

     1  // Copyright 2020 Chaos Mesh Authors.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package main
    15  
    16  import (
    17  	"flag"
    18  	"fmt"
    19  	"os"
    20  	"strings"
    21  
    22  	"github.com/chaos-mesh/chaos-mesh/pkg/time/utils"
    23  
    24  	"github.com/go-logr/zapr"
    25  	"go.uber.org/zap"
    26  
    27  	"github.com/chaos-mesh/chaos-mesh/pkg/ptrace"
    28  
    29  	"github.com/chaos-mesh/chaos-mesh/pkg/version"
    30  
    31  	"github.com/chaos-mesh/chaos-mesh/pkg/time"
    32  )
    33  
    34  var (
    35  	pid           int
    36  	secDelta      int64
    37  	nsecDelta     int64
    38  	printVersion  bool
    39  	clockIdsSlice string
    40  )
    41  
    42  func initFlag() {
    43  	flag.IntVar(&pid, "pid", 0, "pid of target program")
    44  	flag.Int64Var(&secDelta, "sec_delta", 0, "delta time of sec field")
    45  	flag.Int64Var(&nsecDelta, "nsec_delta", 0, "delta time of nsec field")
    46  	flag.StringVar(&clockIdsSlice, "clk_ids", "CLOCK_REALTIME", "all affected clock ids split with \",\"")
    47  	flag.BoolVar(&printVersion, "version", false, "print version information and exit")
    48  
    49  	flag.Parse()
    50  }
    51  
    52  func main() {
    53  	initFlag()
    54  
    55  	version.PrintVersionInfo("watchmaker")
    56  
    57  	if printVersion {
    58  		os.Exit(0)
    59  	}
    60  
    61  	zapLog, err := zap.NewDevelopment()
    62  	if err != nil {
    63  		panic(fmt.Sprintf("error while creating zap logger: %v", err))
    64  	}
    65  	log := zapr.NewLogger(zapLog)
    66  	ptrace.RegisterLogger(log.WithName("ptrace"))
    67  	time.RegisterLogger(log.WithName("time"))
    68  
    69  	clkIds := strings.Split(clockIdsSlice, ",")
    70  	mask, err := utils.EncodeClkIds(clkIds)
    71  	if err != nil {
    72  		log.Error(err, "error while converting clock ids to mask")
    73  		os.Exit(1)
    74  	}
    75  	log.Info("get clock ids mask", "mask", mask)
    76  
    77  	err = time.ModifyTime(pid, secDelta, nsecDelta, mask)
    78  
    79  	if err != nil {
    80  		log.Error(err, "error while modifying time", "pid", pid, "secDelta", secDelta, "nsecDelta", nsecDelta, "mask", mask)
    81  	}
    82  }
    83