You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

_event_writer.py 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Writes events to disk in a logdir."""
  16. import os
  17. import time
  18. import stat
  19. from mindspore import log as logger
  20. from ..._c_expression import EventWriter_
  21. from ._summary_adapter import package_init_event
  22. class _WrapEventWriter(EventWriter_):
  23. """
  24. Wrap the c++ EventWriter object.
  25. Args:
  26. full_file_name (str): Include directory and file name.
  27. """
  28. def __init__(self, full_file_name):
  29. if full_file_name is not None:
  30. EventWriter_.__init__(self, full_file_name)
  31. class EventRecord:
  32. """
  33. Creates a `EventFileWriter` and write event to file.
  34. Args:
  35. full_file_name (str): Summary event file path and file name.
  36. flush_time (int): The flush seconds to flush the pending events to disk. Default: 120.
  37. """
  38. def __init__(self, full_file_name: str, flush_time: int = 120):
  39. self.full_file_name = full_file_name
  40. # The first event will be flushed immediately.
  41. self.flush_time = flush_time
  42. self.next_flush_time = 0
  43. # create event write object
  44. self.event_writer = self._create_event_file()
  45. self._init_event_file()
  46. # count the events
  47. self.event_count = 0
  48. def _create_event_file(self):
  49. """Create the event write file."""
  50. with open(self.full_file_name, 'w'):
  51. os.chmod(self.full_file_name, stat.S_IWUSR | stat.S_IRUSR)
  52. # create c++ event write object
  53. event_writer = _WrapEventWriter(self.full_file_name)
  54. return event_writer
  55. def _init_event_file(self):
  56. """Send the init event to file."""
  57. self.event_writer.Write((package_init_event()).SerializeToString())
  58. self.flush()
  59. return True
  60. def write_event_to_file(self, event_str):
  61. """Write the event to file."""
  62. self.event_writer.Write(event_str)
  63. def get_data_count(self):
  64. """Return the event count."""
  65. return self.event_count
  66. def flush_cycle(self):
  67. """Flush file by timer."""
  68. self.event_count = self.event_count + 1
  69. # Flush the event writer every so often.
  70. now = int(time.time())
  71. if now > self.next_flush_time:
  72. self.flush()
  73. # update the flush time
  74. self.next_flush_time = now + self.flush_time
  75. def count_event(self):
  76. """Count event."""
  77. logger.debug("Write the event count is %r", self.event_count)
  78. self.event_count = self.event_count + 1
  79. return self.event_count
  80. def flush(self):
  81. """Flush the event file to disk."""
  82. self.event_writer.Flush()
  83. def close(self):
  84. """Flush the event file to disk and close the file."""
  85. self.flush()
  86. self.event_writer.Shut()