Skip to content

Commit 2e24e35

Browse files
committed
[poed]Change timestamp calculate method and blocking action in cfg save/load
1. Change timestamp calculate method in unix timestamp format 2. When using the poecli cfg save/load function, user must wait poe agent until the chip state or runtime cfg updated. 3. Check ipc file (/run/poe_ipc_event) before open it, make sure it's named pipe. Signed-off-by: leon.chiang <[email protected]>
1 parent 075be47 commit 2e24e35

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

dentos-poe-agent/opt/poeagent/bin/poecli.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import imp
2525
import sys
2626
import subprocess
27+
import stat
2728
import os
2829
import argparse
2930
import time
@@ -422,8 +423,13 @@ def is_poed_alive(self):
422423

423424
def send_ipc_event(self, action=POECLI_SET):
424425
try:
425-
with open(POE_IPC_EVT, "w") as f:
426-
f.write(action)
426+
if check_file(POE_IPC_EVT) == True:
427+
if stat.S_ISFIFO(os.stat(POE_IPC_EVT).st_mode) == False:
428+
print_stderr(
429+
"Invalid IPC channel: {0}".format(POE_IPC_EVT))
430+
return
431+
with open(POE_IPC_EVT, "w") as f:
432+
f.write(action)
427433
except Exception as e:
428434
pass
429435

@@ -500,7 +506,15 @@ def main(argv):
500506
if set_flag == True and poed_alive == True:
501507
poecli.send_ipc_event()
502508
elif len(cfg_action)>0 and poed_alive == True:
509+
touch_file(POED_BUSY_FLAG)
503510
poecli.send_ipc_event(cfg_action)
511+
if wait_poed_busy(title="Wait cfg action", timeout=SAVE_FILE_TIMEOUT) == True:
512+
print_stderr("done")
513+
if check_file(POED_BUSY_FLAG):
514+
remove_file(POED_BUSY_FLAG)
515+
516+
517+
504518

505519
if __name__ == '__main__':
506520
main(sys.argv)

dentos-poe-agent/opt/poeagent/bin/poed.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from pathlib import Path
2323
import os
24+
import stat
2425
import sys
2526
import errno
2627
import threading
@@ -82,13 +83,15 @@ def is_valid_gen_info(self, gen_info):
8283
self.is_valid_poe_cfg_ver(gen_info[POE_CFG_VER])
8384

8485
def is_increasing_time_sequence(self, t1, t2):
85-
tDelta = datetime.strptime(t2, TIME_FMT) - \
86-
datetime.strptime(t1, TIME_FMT)
87-
result1 =(tDelta.days > 0 or tDelta.seconds > 0)
88-
result2 =(tDelta.days * tDelta.seconds) >= 0
89-
# print_stderr("is_increasing_time_sequence(self, t1, t2): result1={0},result2={1} ".format(str(result1),
90-
# str(result2)))
91-
return result1 and result2
86+
unixtime1 = time.mktime(datetime.strptime(t1, TIME_FMT).timetuple())
87+
unixtime2 = time.mktime(datetime.strptime(t2, TIME_FMT).timetuple())
88+
unixtime_diff = unixtime2-unixtime1
89+
# print_stderr(
90+
# "unixtime diff: {0}-{1}={2}".format(unixtime2, unixtime1, (unixtime2-unixtime1)))
91+
if unixtime_diff >=0:
92+
return True
93+
else:
94+
return False
9295
def is_valid_timestamp(self, timestamp):
9396
last_save_time = timestamp[LAST_SAVE_TIME]
9497
last_set_time = timestamp[LAST_SET_TIME]
@@ -312,6 +315,7 @@ def save_curerent_runtime(self):
312315
copyfile(self.runtime_cfg.path(),
313316
self.permanent_cfg.path())
314317

318+
315319
def autosave_main(self):
316320
global thread_flag
317321
self.log.info("Start autosave thread")
@@ -320,6 +324,7 @@ def autosave_main(self):
320324
while thread_flag is True:
321325
try:
322326
if self.rt_counter >= self.cfg_update_intvl_rt:
327+
# print_stderr("Load chip state")
323328
cfg_data = self.collect_running_state()
324329
if self.failsafe_flag == False:
325330
if self.save_poe_cfg(self.runtime_cfg, cfg_data) == True:
@@ -413,11 +418,17 @@ def get_poe_agent_stae(self):
413418
return self.poe_agent_state
414419

415420
def create_poe_set_ipc(self):
421+
try:
422+
if Path(POE_IPC_EVT).exists() and stat.S_ISFIFO(os.stat(POE_IPC_EVT).st_mode) == False:
423+
#Remove non-namedpipe file
424+
remove_file(POE_IPC_EVT)
425+
except:
426+
pass
416427
try:
417428
os.mkfifo(POE_IPC_EVT)
418429
except OSError as oe:
419430
if oe.errno != errno.EEXIST:
420-
self.log.err("Failed to open named pipe: %s" % str(e))
431+
self.log.err("Failed to open named pipe: %s" % str(oe))
421432

422433
def get_prev_pid():
423434
return int(open(POED_PID_PATH, 'r').read())
@@ -504,10 +515,10 @@ def main(argv):
504515
if data == POECLI_SET:
505516
pa.update_set_time()
506517
pa.log.info("Receive a set event from poecli!")
507-
if pa.rt_counter <pa.cfg_update_intvl_rt:
508-
pa.log.info("Reset rt_counter timing: {0}".format(
509-
str(pa.cfg_update_intvl_rt)))
510-
pa.rt_counter = pa.cfg_update_intvl_rt
518+
# if pa.rt_counter <pa.cfg_update_intvl_rt:
519+
pa.log.info("Reset rt_counter timing: {0}".format(
520+
str(pa.cfg_update_intvl_rt-1)))
521+
pa.rt_counter = pa.cfg_update_intvl_rt-1
511522
break
512523
elif data == POECLI_CFG:
513524
pa.log.info("Receive a cfg event from poecli!")
@@ -524,6 +535,15 @@ def main(argv):
524535
apply = data_list[3]
525536
pa.log.info("CFG Apply: {0}".format(apply))
526537
if action==POED_SAVE_ACTION:
538+
touch_file(POED_BUSY_FLAG)
539+
pa.rt_counter = 0
540+
cfg_data = pa.collect_running_state()
541+
if pa.save_poe_cfg(pa.runtime_cfg, cfg_data) == True:
542+
pa.log.info(
543+
"Load chip state to runtime completed.")
544+
else:
545+
pa.log.warn(
546+
"Failed to save cfg data in autosave routine!")
527547
if file == None:
528548
pa.log.info(
529549
"CFG Save: Save runtime setting to persistent file")
@@ -533,16 +553,19 @@ def main(argv):
533553
file)
534554
pa.log.info(
535555
"CFG Save: Save runtime setting to {0}".format(file))
536-
elif action==POED_LOAD_ACTION:
556+
remove_file(POED_BUSY_FLAG)
557+
elif action == POED_LOAD_ACTION:
537558
if file == None:
538559
pa.log.info(
539560
"CFG Load: Load persistent file")
540561
result = pa.load_poe_cfg(pa.permanent_cfg)
562+
remove_file(POED_BUSY_FLAG)
541563
else:
542564
pa.log.info(
543565
"CFG Load: Load cfg file from {0}".format(file))
544566
temp_cfg = PoeConfig(file, pa.plat_name)
545567
result = pa.load_poe_cfg(temp_cfg)
568+
remove_file(POED_BUSY_FLAG)
546569
if result == True:
547570
pa.update_set_time()
548571
break

dentos-poe-agent/opt/poeagent/inc/poe_common.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
POED_BUSY_FLAG = "/run/.poed_busy"
112112
POED_EXIT_FLAG = "/run/.poed_exit"
113113
FILEFLAG_RETRY = 5
114+
SAVE_FILE_TIMEOUT = 20
114115

115116
def print_stderr(msg,end="\n",flush=True):
116117
sys.stderr.write(msg+end)
@@ -228,15 +229,17 @@ def check_file(file_path):
228229
return False
229230

230231

231-
def wait_poed_busy(timeout=FILEFLAG_RETRY):
232+
def wait_poed_busy(timeout=FILEFLAG_RETRY, title="poe agent busy"):
232233
ret = check_file(POED_BUSY_FLAG)
234+
if ret == True:
235+
print_stderr("\r"+title+".", end='')
233236
while ret == True:
234237
ret = check_file(POED_BUSY_FLAG)
235-
print_stderr("\rpoe agent busy...")
236238
if timeout > 0:
239+
print_stderr(".", end='')
237240
timeout -= 1
238241
else:
239-
print_stderr("\r\rpoe agent busy...timeout")
242+
print_stderr("timeout")
240243
return False
241244
time.sleep(1)
242245
return True

0 commit comments

Comments
 (0)