Example #1
0
// Time returns the current time, in whole seconds and
// fractional nanoseconds, plus an Error if any. The current
// time is thus 1e9*sec+nsec, in nanoseconds.  The zero of
// time is the Unix epoch.
func Time() (sec int64, nsec int64, err Error) {
	var tv syscall.Timeval
	if e := syscall.Gettimeofday(&tv); iserror(e) {
		return 0, 0, NewSyscallError("gettimeofday", e)
	}
	return int64(tv.Sec), int64(tv.Usec) * 1000, err
}
Example #2
0
func TestDarwinGettimeofday(t *testing.T) {
	tv := &syscall.Timeval{}
	if err := syscall.Gettimeofday(tv); err != nil {
		t.Fatal(err)
	}
	if tv.Sec == 0 && tv.Usec == 0 {
		t.Fatal("Sec and Usec both zero")
	}
}
Example #3
0
func TestGettimeofday(t *testing.T) {
	if runtime.GOOS == "nacl" {
		t.Skip("not implemented on nacl")
	}
	tv := &syscall.Timeval{}
	if err := syscall.Gettimeofday(tv); err != nil {
		t.Fatal(err)
	}
	if tv.Sec == 0 && tv.Usec == 0 {
		t.Fatal("Sec and Usec both zero")
	}
}
Example #4
0
func Afunction(client *godis.Client, wg sync.WaitGroup) {
	var ts, te syscall.Timeval
	syscall.Gettimeofday(&ts)
	start := (int64(ts.Sec)*1e3 + int64(ts.Usec)/1e3)

	h := func(v godis.RespInfo) (interface{}, error) {
		defer wg.Add(-1)
		syscall.Gettimeofday(&te)
		end := (int64(te.Sec)*1e3 + int64(te.Usec)/1e3)
		cost := end - start
		atomic.AddInt64(&ops, cost)
		log.Println("cost: %d", ops)
		return nil, errors.New("Ssss")
	}

	n := make(godis.ProtoType)
	n["a"] = 1
	n["b"] = 2
	client.Call("hello", &h, n)

}
Example #5
0
// return min, sec and µsec since specified starttime
func getTimePassed(starttime syscall.Timeval) (min, sec, µsec int) {
	var time syscall.Timeval
	syscall.Gettimeofday(&time)
	sec = int(time.Sec - starttime.Sec)
	µsec = int(time.Usec - starttime.Usec)
	if µsec < 0 {
		sec--
		µsec = 1000000 + µsec
	}
	min = sec / 60
	sec = sec % 60
	return
}
Example #6
0
func Gettimeofday() (ts HighResTimestamp, err error) {
	var tval syscall.Timeval
	syscall.Gettimeofday(&tval)

	f, err := strconv.ParseFloat(fmt.Sprintf("%d.%d", tval.Sec, tval.Usec), 64)
	if err != nil {
		fmt.Printf("error creating timestamp: `%s`", err)
		return
	}

	ts = HighResTimestamp(f)
	return
}
Example #7
0
func (s *service) Status() (*SvStatus, error) {
	status, err := s.status()
	if err != nil {
		return nil, err
	}

	var pid int
	pid = int(status[posPidEnd])
	for i := posPidEnd - 1; i >= posPidStart; i-- {
		pid <<= 8
		pid += int(status[i])
	}

	tai := int64(status[posTimeStart])
	for i := posTimeStart + 1; i <= posTimeEnd; i++ {
		tai <<= 8
		tai += int64(status[i])
	}
	state := status[posState] // 0: down, 1: run, 2: finish

	tv := &syscall.Timeval{}
	if err := syscall.Gettimeofday(tv); err != nil {
		return nil, err
	}
	sS := SvStatus{
		Pid:        pid,
		Timestamp:  time.Unix(tai-taiOffset, 0), // FIXME: do we just select the wrong slice?
		Duration:   int(int64(tv.Sec) - (tai - taiOffset)),
		State:      int(state),
		NormallyUp: s.NormallyUp(),
	}

	switch status[posWant] {
	case 'u':
		sS.Want = StateUp
	case 'd':
		sS.Want = StateDown
	}

	return &sS, nil
}
Example #8
0
func (guitar *SimpleGuitar) Reset() {
	var now syscall.Timeval
	syscall.Gettimeofday(&now)
	defaultButtonState := ButtonState{now, false}

	guitar.green = defaultButtonState
	guitar.red = defaultButtonState
	guitar.yellow = defaultButtonState
	guitar.blue = defaultButtonState
	guitar.orange = defaultButtonState
	guitar.start = defaultButtonState
	guitar.sel = defaultButtonState
	guitar.main = defaultButtonState
	guitar.strum = STRUM_NONE

	guitar.chordMap = &ChordMap{}
	guitar.chordMap.Init()

	guitar.mode = &SimpleGuitarMode{}
	guitar.mode.EnterMode(guitar)
}
Example #9
0
// run the algo, print some output and catch if won.
// straightAhead: true: new direction are initialized with current dir, false: init with 0
func Run(e engine.Engine, single bool, outputFreq int32, printSurface bool, straightAhead bool, threads int) {
	steps, solutions, solSteps = 0, 0, []int32{}
	numWorkers = 0
	running = true
	cDone = make(chan int8, threads) // queue for threads
	cHistory = make(chan bool, 1)    // mutex on global history object

	// preprocessing
	MarkDeadFields(&e.Surface)
	e.Print()

	// init time counter
	syscall.Gettimeofday(&starttime)

	// ???
	//	runtime.GOMAXPROCS(runtime.NumCPU())

	// init path
	path := Path{}
	path.Push(engine.NO_DIRECTION)

	// create history store and save initial constellation
	history = newHistoryTree(-1, -1)
	newHist := e.GetBoxesAndX()
	addHistory(&history, newHist)

	// prepare for starting workers
	wg.Add(1)
	cDone <- 1
	go runWorker(e, path, threads, single, outputFreq, printSurface, straightAhead)

	// wait for all workers to finish
	wg.Wait()
	//	time.Sleep(1 * time.Second)

	// print result
	min, sec, µsec := getTimePassed(starttime)
	log.A("Run finished with %d steps after %dm %ds %dµs.\n%d solutions found at following steps:\n%d\n", steps, min, sec, µsec, solutions, solSteps)
}
// 验证msg格式
func verify(s []byte) (producer *sarama.Producer, topic string, msg string, err error) {

	var j map[string]interface{}
	err = json.Unmarshal(s, &j)
	if err != nil {
		return
	}

	product, ok := j["product"].(string)
	if !ok {
		err = errors.New("product not find in message")
		return
	}
	service, ok := j["service"].(string)
	if !ok {
		err = errors.New("service not find in message")
		return
	}

	topic = product + "_" + service + "_topic"

	j["topic"] = topic

	syscall.Gettimeofday(&TV)
	j["collector_time"] = (int64(TV.Sec)*1e3 + int64(TV.Usec)/1e3)

	producer, err = selectProducer(j)
	if nil != err {
		return
	}

	msgInByte, err := json.Marshal(j)

	msg = string(msgInByte)

	return
}
Example #11
0
func getMillTime() int64 {
	var tv syscall.Timeval
	syscall.Gettimeofday(&tv)
	return (int64(tv.Sec)*1e3 + int64(tv.Usec)/1e3)
}
Example #12
0
func now() syscall.Timeval {
	var now syscall.Timeval
	syscall.Gettimeofday(&now)
	return now
}
Example #13
0
// UnixMilliseconds returns the current Unix timestamp in
// milliseconds since the epoch
func UnixMilliseconds() int64 {
	var tv syscall.Timeval
	syscall.Gettimeofday(&tv)
	return (int64(tv.Sec)*1e3 + int64(tv.Usec)/1e3)
}
Example #14
0
func main() {
	tm.Init()
	tm.Clear(tm.ColorDefault, tm.ColorDefault)

	gameOver := false
	playerWon := false

	emitter := emission.NewEmitter()
	emitter.On(event.EVENT_KEY_CTRL_C, func() {
		gameOver = true
	})

	emitter.On(event.EVENT_PLAYER_WINS, func() {
		playerWon = true
		gameOver = true
	})

	emitter.On(event.EVENT_PLAYER_LOSES, func() {
		gameOver = true
	})

	keyboard := new(keyboard.Keyboard)
	keyboard.Init(emitter)

	mainboard := new(gameboard.Mainboard)
	mainboard.Init(1, 1, 50, 20)

	player := new(player.Player)
	player.Init(emitter, 1, 50, 20)

	alienManager := new(alien.AlienManager)
	alienManager.Init(emitter, 1, 1, 50, 20)

	var currentTime syscall.Timeval

	eventQueue := make(chan tm.Event)
	go func() {
		for {
			eventQueue <- tm.PollEvent()
		}
	}()

	timer := time.NewTimer(time.Duration(time.Millisecond * 60))

	for gameOver == false {
		select {
		case ev := <-eventQueue:
			if ev.Type == tm.EventKey {
				keyboard.HandleEvent(ev.Key)
			}
		case <-timer.C:
			syscall.Gettimeofday(&currentTime)

			emitter.Emit(event.EVENT_HEARTBEAT, (int64(currentTime.Sec)*1e3 + int64(currentTime.Usec)/1e3))
			timer.Reset(time.Duration(time.Millisecond * 60))
		}

		tm.Flush()
	}

	tm.Close()

	if playerWon == true {
		fmt.Println("You win!")
	} else {
		fmt.Println("You lose :(")
	}
}
Example #15
0
func NowMicroseconds() int64 {
	tv := syscall.Timeval{}
	syscall.Gettimeofday(&tv)
	return (int64(tv.Sec)*1e3 + int64(tv.Usec)/1e3)
}