...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/time/utils/utils.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/time/utils

     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 utils
    15  
    16  import "fmt"
    17  
    18  // EncodeClkIds will convert array of clk ids into a mask
    19  func EncodeClkIds(clkIds []string) (uint64, error) {
    20  	mask := uint64(0)
    21  
    22  	for _, id := range clkIds {
    23  		// refer to `uapi/linux/time.h`
    24  		switch id {
    25  		case "CLOCK_REALTIME":
    26  			mask |= 1 << 0
    27  		case "CLOCK_MONOTONIC":
    28  			mask |= 1 << 1
    29  		case "CLOCK_PROCESS_CPUTIME_ID":
    30  			mask |= 1 << 2
    31  		case "CLOCK_THREAD_CPUTIME_ID":
    32  			mask |= 1 << 3
    33  		case "CLOCK_MONOTONIC_RAW":
    34  			mask |= 1 << 4
    35  		case "CLOCK_REALTIME_COARSE":
    36  			mask |= 1 << 5
    37  		case "CLOCK_MONOTONIC_COARSE":
    38  			mask |= 1 << 6
    39  		case "CLOCK_BOOTTIME":
    40  			mask |= 1 << 7
    41  		case "CLOCK_REALTIME_ALARM":
    42  			mask |= 1 << 8
    43  		case "CLOCK_BOOTTIME_ALARM":
    44  			mask |= 1 << 9
    45  		default:
    46  			return 0, fmt.Errorf("unknown clock id %s", id)
    47  		}
    48  	}
    49  
    50  	return mask, nil
    51  }
    52