752 } |
752 } |
753 |
753 |
754 cpu_info = *_cpu_info; // shallow copy assignment |
754 cpu_info = *_cpu_info; // shallow copy assignment |
755 return OS_OK; |
755 return OS_OK; |
756 } |
756 } |
|
757 |
|
758 class NetworkPerformanceInterface::NetworkPerformance : public CHeapObj<mtInternal> { |
|
759 friend class NetworkPerformanceInterface; |
|
760 private: |
|
761 NetworkPerformance(); |
|
762 NetworkPerformance(const NetworkPerformance& rhs); // no impl |
|
763 NetworkPerformance& operator=(const NetworkPerformance& rhs); // no impl |
|
764 bool initialize(); |
|
765 ~NetworkPerformance(); |
|
766 int network_utilization(NetworkInterface** network_interfaces) const; |
|
767 }; |
|
768 |
|
769 NetworkPerformanceInterface::NetworkPerformance::NetworkPerformance() { |
|
770 |
|
771 } |
|
772 |
|
773 bool NetworkPerformanceInterface::NetworkPerformance::initialize() { |
|
774 return true; |
|
775 } |
|
776 |
|
777 NetworkPerformanceInterface::NetworkPerformance::~NetworkPerformance() { |
|
778 |
|
779 } |
|
780 |
|
781 int NetworkPerformanceInterface::NetworkPerformance::network_utilization(NetworkInterface** network_interfaces) const |
|
782 { |
|
783 kstat_ctl_t* ctl = kstat_open(); |
|
784 if (ctl == NULL) { |
|
785 return OS_ERR; |
|
786 } |
|
787 |
|
788 NetworkInterface* ret = NULL; |
|
789 for (kstat_t* k = ctl->kc_chain; k != NULL; k = k->ks_next) { |
|
790 if (strcmp(k->ks_class, "net") != 0) { |
|
791 continue; |
|
792 } |
|
793 if (strcmp(k->ks_module, "link") != 0) { |
|
794 continue; |
|
795 } |
|
796 |
|
797 if (kstat_read(ctl, k, NULL) == -1) { |
|
798 return OS_ERR; |
|
799 } |
|
800 |
|
801 uint64_t bytes_in = UINT64_MAX; |
|
802 uint64_t bytes_out = UINT64_MAX; |
|
803 for (int i = 0; i < k->ks_ndata; ++i) { |
|
804 kstat_named_t* data = &reinterpret_cast<kstat_named_t*>(k->ks_data)[i]; |
|
805 if (strcmp(data->name, "rbytes64") == 0) { |
|
806 bytes_in = data->value.ui64; |
|
807 } |
|
808 else if (strcmp(data->name, "obytes64") == 0) { |
|
809 bytes_out = data->value.ui64; |
|
810 } |
|
811 } |
|
812 |
|
813 if ((bytes_in != UINT64_MAX) && (bytes_out != UINT64_MAX)) { |
|
814 NetworkInterface* cur = new NetworkInterface(k->ks_name, bytes_in, bytes_out, ret); |
|
815 ret = cur; |
|
816 } |
|
817 } |
|
818 |
|
819 *network_interfaces = ret; |
|
820 |
|
821 return OS_OK; |
|
822 } |
|
823 |
|
824 NetworkPerformanceInterface::NetworkPerformanceInterface() { |
|
825 _impl = NULL; |
|
826 } |
|
827 |
|
828 NetworkPerformanceInterface::~NetworkPerformanceInterface() { |
|
829 if (_impl != NULL) { |
|
830 delete _impl; |
|
831 } |
|
832 } |
|
833 |
|
834 bool NetworkPerformanceInterface::initialize() { |
|
835 _impl = new NetworkPerformanceInterface::NetworkPerformance(); |
|
836 return _impl != NULL && _impl->initialize(); |
|
837 } |
|
838 |
|
839 int NetworkPerformanceInterface::network_utilization(NetworkInterface** network_interfaces) const { |
|
840 return _impl->network_utilization(network_interfaces); |
|
841 } |