File indexing completed on 2024-04-28 17:02:20

0001 /*
0002    This file is part of Massif Visualizer
0003 
0004    Copyright 2010 Milian Wolff <mail@milianw.de>
0005 
0006    This library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Lesser General Public
0008    License as published by the Free Software Foundation; either
0009    version 2.1 of the License, or (at your option) version 3, or any
0010    later version accepted by the membership of KDE e.V. (or its
0011    successor approved by the membership of KDE e.V.), which shall
0012    act as a proxy defined in Section 6 of version 3 of the license.
0013 
0014    This library is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0017    Lesser General Public License for more details.
0018 
0019    You should have received a copy of the GNU Lesser General Public
0020    License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "parser.h"
0024 
0025 #include "filedata.h"
0026 #include "parserprivate.h"
0027 #include "snapshotitem.h"
0028 
0029 #include <QtCore/QIODevice>
0030 
0031 using namespace Massif;
0032 
0033 Parser::Parser()
0034     : m_errorLine(-1)
0035 {
0036 }
0037 
0038 Parser::~Parser()
0039 {
0040 }
0041 
0042 FileData* Parser::parse(QIODevice* file, const QStringList& customAllocators, QAtomicInt* shouldStop)
0043 {
0044     Q_ASSERT(file->isOpen());
0045     Q_ASSERT(file->isReadable());
0046 
0047     QScopedPointer<FileData> data(new FileData);
0048 
0049     ParserPrivate p(this, file, data.data(), customAllocators, shouldStop);
0050 
0051     if (p.error()) {
0052         m_errorLine = p.errorLine();
0053         m_errorLineString = p.errorLineString();
0054         return 0;
0055     } else {
0056         m_errorLine = -1;
0057         m_errorLineString.clear();
0058     }
0059 
0060     // when a massif run gets terminated (^C) the snapshot data might be wrong,
0061     // hence just always ensure we pick the proper peak ourselves
0062     if (!data->snapshots().isEmpty()) {
0063         foreach ( SnapshotItem* snapshot, data->snapshots() ) {
0064             if (!snapshot->heapTree()) {
0065                 // peak should have detailed info
0066                 continue;
0067             }
0068             if (!data->peak() || snapshot->cost() > data->peak()->cost()) {
0069                 data->setPeak(snapshot);
0070             }
0071         }
0072         // still not found? pick any other snapshot
0073         if (!data->peak()) {
0074             foreach( SnapshotItem* snapshot, data->snapshots() ) {
0075                 if (!data->peak() || snapshot->cost() > data->peak()->cost()) {
0076                     data->setPeak(snapshot);
0077                 }
0078             }
0079         }
0080     }
0081     // peak might still be zero if we have no snapshots, should be handled in the UI then
0082 
0083     return data.take();
0084 }
0085 
0086 int Parser::errorLine() const
0087 {
0088     return m_errorLine;
0089 }
0090 
0091 QByteArray Parser::errorLineString() const
0092 {
0093     return m_errorLineString;
0094 }
0095 
0096 void Parser::setProgress(int value)
0097 {
0098     emit progress(value);
0099 }