Skip to content

Commit 03ed088

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 03ed088

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
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: 32 additions & 12 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:
@@ -414,6 +419,9 @@ def get_poe_agent_stae(self):
414419

415420
def create_poe_set_ipc(self):
416421
try:
422+
if stat.S_ISFIFO(os.stat(POE_IPC_EVT).st_mode) == False:
423+
#Remove non-namedpipe file
424+
remove_file(POE_IPC_EVT)
417425
os.mkfifo(POE_IPC_EVT)
418426
except OSError as oe:
419427
if oe.errno != errno.EEXIST:
@@ -504,10 +512,10 @@ def main(argv):
504512
if data == POECLI_SET:
505513
pa.update_set_time()
506514
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
515+
# if pa.rt_counter <pa.cfg_update_intvl_rt:
516+
pa.log.info("Reset rt_counter timing: {0}".format(
517+
str(pa.cfg_update_intvl_rt-1)))
518+
pa.rt_counter = pa.cfg_update_intvl_rt-1
511519
break
512520
elif data == POECLI_CFG:
513521
pa.log.info("Receive a cfg event from poecli!")
@@ -524,6 +532,15 @@ def main(argv):
524532
apply = data_list[3]
525533
pa.log.info("CFG Apply: {0}".format(apply))
526534
if action==POED_SAVE_ACTION:
535+
touch_file(POED_BUSY_FLAG)
536+
pa.rt_counter = 0
537+
cfg_data = pa.collect_running_state()
538+
if pa.save_poe_cfg(pa.runtime_cfg, cfg_data) == True:
539+
pa.log.info(
540+
"Load chip state to runtime completed.")
541+
else:
542+
pa.log.warn(
543+
"Failed to save cfg data in autosave routine!")
527544
if file == None:
528545
pa.log.info(
529546
"CFG Save: Save runtime setting to persistent file")
@@ -533,16 +550,19 @@ def main(argv):
533550
file)
534551
pa.log.info(
535552
"CFG Save: Save runtime setting to {0}".format(file))
536-
elif action==POED_LOAD_ACTION:
553+
remove_file(POED_BUSY_FLAG)
554+
elif action == POED_LOAD_ACTION:
537555
if file == None:
538556
pa.log.info(
539557
"CFG Load: Load persistent file")
540558
result = pa.load_poe_cfg(pa.permanent_cfg)
559+
remove_file(POED_BUSY_FLAG)
541560
else:
542561
pa.log.info(
543562
"CFG Load: Load cfg file from {0}".format(file))
544563
temp_cfg = PoeConfig(file, pa.plat_name)
545564
result = pa.load_poe_cfg(temp_cfg)
565+
remove_file(POED_BUSY_FLAG)
546566
if result == True:
547567
pa.update_set_time()
548568
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)